2016-11-22 51 views
1

我在command.in文件夾中創建了一個新類App \ Console \ Commands;laravel:調度程序無法找到我的控制器

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 

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

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

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

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
     return $schedule->call('[email protected]')->everyMinute(); 
    } 
} 

,並在kern.php我寫了下面的代碼

<?php 

namespace App\Console; 

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

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

    /** 
    * Define the application's command schedule. 
    * 
    * @param \Illuminate\Console\Scheduling\Schedule $schedule 
    * @return void 
    */ 
    protected function schedule(Schedule $schedule) 
    { 

     return $schedule->call('')->everyMinute(); 
    } 

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

我應該寫在這裏的控制器類?或者我應該寫什麼?

return $schedule->call('')->everyMinute(); 

或者我應該創建一個新的類,它具有與控制器相同的功能?

+1

控制器用於Web請求,您需要創建一個控制檯命令類 –

+0

您確定要在這裏使用「受保護的函數」嗎? – Frondor

回答

2

您應該將控制器視爲您的應用程序的一個傳輸層。任何實際的功能邏輯都應該進入你的域類。

嘗試以它消耗的方式重構您的控制器功能,例如服務類。然後創建一個使用相同服務類的命令類,提供另一種觸發功能的方法。

這樣,您就可以將功能封裝在服務類中。控制器和命令類都只是將這個類用作傳輸方式; HTTP請求和CLI命令。

重構完成後,您可以安排您的命令每分鐘運行一次。