从控制台调用 laravel artisan 命令

Calling laravel artisan command from console

我正在尝试自学 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 调用此命令,以便结果为 "Test has fired."?

到 运行 手动命令:php artisan command:name.

删除你的 fire 函数,你可以在 handle 函数中处理它。

在内核中修复您的计划功能 class

class Kernel extends ConsoleKernel
{
    ....

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

要配置您的日程安排,请阅读以下内容: https://laravel.com/docs/5.4/scheduling