2016-11-22 83 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命令。

重构完成后,您可以安排您的命令每分钟运行一次。

相关问题