0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LaravelAdvent Calendar 2022

Day 14

artisan commandで可変長引数【Laravel】Rest variable length arguments

Last updated at Posted at 2022-12-14

$signature = 'command:name {your_command?*}' のように?*を追加するだけ カンタン

app/Console/Commands/shell_exec.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class shell_exec extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name {your_command?*}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $command = join(' ', $this->argument('your_command'));
        if ($command) {
            $output = shell_exec($command);
            if ($output) $this->line($output);
        }
        return 0;
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?