Route execution (controller / action) using PHP CLI and CLI request discovery

Is there a way in Laravel 4 to launch 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 comes from the CLI, so is there a way to identify the request as a CLI request?

The Laravel documentation on this site seems to suggest that there is a Request :: cli () method to determine if the current request is being executed through the Artisan CLI, but when I used the method in Laravel 4, it throws an error:

Call the undefined method Illuminate \ Http \ Request :: cli ()

Basically, I just migrated from CakePHP to Laravel and would like to do something similar to what is described in this article (for CakePHP): Invoking controller actions from cron and command line

I understand that I can work with Larvel 4 Artisan Commands, but is this an approach I would like to use? And if so, how?

+4
source share
1 answer

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

As for launching the @action controller from the console, you can use curl or wget to request one of your routes, but I believe that using the team of custom wizards is the right way to do this. Your controllers are classes, so you can create them and use them as you wish within your artisan command:

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

Watch this video for easy development of your own wizard teams.

+9
source

Source: https://habr.com/ru/post/1500445/


All Articles