2017-02-09 8 views
0

私は自分自身でLaravelコマンドを教えて、後で私はそれらをスケジューリングに使用します。これは私のカーネルファイルです:コンソールからlaravel artisanコマンドを呼び出す

namespace App\Console; 

    use Illuminate\Console\Scheduling\Schedule; 
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 

    class Kernel extends ConsoleKernel 
    { 
     /** 
     * The Artisan commands provided by your application. 
     * 
     * @var array 
     */ 
     protected $commands = [ 
      // 
      'App\Console\Commands\FooCommand',  
     ]; 

     /** 
     * Define the application's command schedule. 
     * 
     * @param \Illuminate\Console\Scheduling\Schedule $schedule 
     * @return void 
     */ 
     protected function schedule(Schedule $schedule) 
     { 
      // $schedule->command('inspire') 
      //   ->hourly(); 
      $schedule->command('App\Console\Commands\FooCommand')->hourly(); 
     } 

     /** 
     * Register the Closure based commands for the application. 
     * 
     * @return void 
     */ 
     protected function commands() 
     { 
      require base_path('routes/console.php'); 
     } 
    } 

そして、これは\アプリケーション\コンソール\コマンド内のコマンドファイル

namespace App\Console\Commands; 

use Illuminate\Console\Command; 

class FooCommand 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() 
    { 
     // 
    } 

    public function fire() 
    { 

     $this->info('Test has fired.'); 
    } 
} 

である私はFooCommandコマンドをテストしたいです。どのようにしてシェルからこのコマンドを呼び出して、結果が "テストが起動しました"か?

答えて

2

手動でコマンドを実行する:php artisan command:name

fire機能を削除すると、handleの内部でこれを処理できます。

は、あなたのスケジュールを設定へのカーネルクラス

class Kernel extends ConsoleKernel 
{ 
    .... 

    protected function schedule(Schedule $schedule) 
    { 
     $schedule->command('command:name') 
      ->hourly(); 
    } 
} 

であなたのスケジュール機能を修正し、これを読んでください:返信用 https://laravel.com/docs/5.4/scheduling

+0

感謝を。 "SecondCommand"のような別のコマンドファイルを追加するとどうなりますか?私はそれをどのように呼びますか? – user7432810

+0

'schedule:run'コマンドは、' schedule'関数で登録されたすべてのコマンドを実行します。 'schedule'コマンドの中に' $ schedule-> command( 'command:name') - > hourly();のような新しいコマンドを追加するだけです。 – MarcosRJJunior

関連する問題