「そりゃそうだ」という話ですが、表題の通りです。
例えば以下のコマンドをLaravelのアプリケーションのディレクトリで実行した場合
php artisan make:console Test
/[アプリケーションのディレクトリ]/app/Console/Commands/ ディレクトリ以下で"Test.php"というソースが出来上がります。
それで、Test.phpの中身がLaravelのバージョンによって異なります。
Laravel5.0
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class Test extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:name';
/**
* 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 mixed
*/
public function fire()
{
//
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}
Laravel5.1
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Test extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* 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 mixed
*/
public function handle()
{
//
}
}
バージョン変更の際に気をつけたいこと
コマンドの記述の方式が変わるため、もしバージョンを変えるとなった場合はソースを少し書き換える必要があります。
$name $signature
宣言するプロパティについて、$description
は同一ですが、$name
に関しては$signature
に変わっています。
protected $name = 'Test';
5.0の場合、$name
にて名前だけを指定し、引数に関してはgetArguments()
側で記述していました。
protected $signature = 'Test {date} {time?}';
5.1からは$signature
にてコマンドの名前に加えて、中括弧{}
で引数を続けます。
その代わりgetArguments()
、getOptions()
が不要になります。
引数は?
を付けると任意の引数になる等の規則があり、詳しくはこちらをご確認ください。
https://readouble.com/laravel/5.1/ja/artisan.html#defining-input-expectations
$name
を残すべきか
$name
は既ににログの出力等で$this->name
というように参照しているのであれば、そのまま残しておく必要があります。
fire()
handle()
実行する内容を記述する場所がfire()
から handle()
に変わります。
そのため、5.0でfire()
内で記述していた内容を、handle()
に移しておく必要があります。