从控制台调用laravel artisan命令

时间:2017-02-09 23:18:02

标签: laravel command artisan

我正在尝试自学Laravel命令,以便以后我用它来安排它们。这是我的内核文件:

    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');
        }
    }

这是\ App \ Console \ Commands

中的命令文件
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命令。如何从shell调用此命令,以便结果为"测试已触发。"?

1 个答案:

答案 0 :(得分:4)

手动运行命令: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