Call laravel controller via command line

后端 未结 5 2066
礼貌的吻别
礼貌的吻别 2020-11-29 03:17

In kohana framework I can call controller via command line using

php5 index.php --uri=controller/method/var1/var2

Is it possible to call co

5条回答
  •  自闭症患者
    2020-11-29 03:53

    There is no way so far (not sure if there will ever be). However you can create your own Artisan Command that can do that. Create a command CallRoute using this:

    php artisan make:console CallRoute
    

    For Laravel 5.3 or greater you need to use make:command instead:

    php artisan make:command CallRoute
    

    This will generate a command class in app/Console/Commands/CallRoute.php. The contents of that class should look like this:

    option('uri'), 'GET');
            $this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
        }
    
        protected function getOptions()
        {
            return [
                ['uri', null, InputOption::VALUE_REQUIRED, 'The path of the route to be called', null],
            ];
        }
    
    }
    

    You then need to register the command by adding it to the $commands array in app/Console/Kernel.php:

    protected $commands = [
        ...,
        'App\Console\Commands\CallRoute',
    ];
    

    You can now call any route by using this command:

    php artisan route:call --uri=/route/path/with/param
    

    Mind you, this command will return a response as it would be sent to the browser, that means it includes the HTTP headers at the top of the output.

提交回复
热议问题