Executing a Route (Controller/Action) using PHP CLI and Detecting a CLI Request

后端 未结 1 637
孤街浪徒
孤街浪徒 2021-01-14 01:07

Is there a way in Laravel 4 to run my controller/action using PHP-CLI? I have a controller/action that I would like to extend to perform an alternative action if the request

1条回答
  •  有刺的猬
    2021-01-14 01:32

    As Rob already said, to determine if the current script is being run in the console use App::runningInConsole() or simple plain PHP php_sapi_name() == 'cli'.

    As for running controller@action from console, you could use curl or wget to request one of your routes but I think the proper way of doing it would be to use a custom artisan command. Your controllers are classes so you can instantiate them and use as you please from within your artisan command:

    $controller = new SomeController;
    $controller->someAction();
    

    Watch this video for an introduction to easily developing your own artisan commands.

    0 讨论(0)
提交回复
热议问题