LoginSignup
1
2

More than 5 years have passed since last update.

【Laravel】php artisan make:console で作られるコマンドのひな形がLaravelのバージョンによって異なります。

Last updated at Posted at 2016-10-11

「そりゃそうだ」という話ですが、表題の通りです。

例えば以下のコマンドをLaravelのアプリケーションのディレクトリで実行した場合

php artisan make:console Test

/[アプリケーションのディレクトリ]/app/Console/Commands/ ディレクトリ以下で"Test.php"というソースが出来上がります。

それで、Test.phpの中身がLaravelのバージョンによって異なります。

Laravel5.0

Test.php
<?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

Test.php
<?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に変わっています。

test.php
protected $name = 'Test';

5.0の場合、$nameにて名前だけを指定し、引数に関してはgetArguments()側で記述していました。

test.php
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()に移しておく必要があります。

1
2
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
1
2