Question:
Is there any way to call a Laravel route inside a controller or console?
Can I dispatch GET or POST route via an artisan command?
Solution:
Suppose you have some named routes like this:
Route::get('/foo/bar','BazController@qux')->name('foo.bar');
Or
Route::post('/foo/bar','BazController@qux')->name('foo.bar');
Step 1:
If you are going to call one of the laravel routes internally, you need first to create a request in this way:
use Illuminate\Http\Request;$request = Request::create('/foo/bar', 'GET');
If you have set a name for your route, that route can be called by:
$request = Request::create(route('foo.bar'), 'GET');
Or in the case of POST request, do this:
$request = Request::create('/foo/bar', 'POST', $params);or$request = Request::create(route('foo.bar'), 'POST', $params);
Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.
https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs
Step 2:
Then handle your request via app( ):
$response = app()->handle($request);
Step 3:
Then get the response like:
$responseBody = $response->getContent();or$responseBody = json_decode($response->getContent(), true);
php artisan command:
If you are going to call or dispatch a Laravel route in artisan command, first make an artisan command by:
php artisan make:command DispatchFooBar --command=dispatch:foobar
Then register it in the Kernel.php file in app/Console as follows:
Then determine the artisan command functionality by filling the DispatchFooBar class with the proper logics. In the handle() method of this class put these:
$request = Request::create(route('foo.bar'), 'GET');$response = app()->handle($request);
and also:
$responseBody = $response->getContent();or$responseBody = json_decode($response->getContent(), true);
Then inside the handle() method but at the end, set a message for when the command is completely done.
$this->info('foo.bar route has been dispatched successfully');
Then run:
php artisan dispatch:foobar
The result will be:
Maybe the route you are trying to access has authentication middleware. If the route is protected by a middleware or is only accessible by an admin, you should insert some more logics to pass the credentials. Something like this is one of the solutions:
Note:
instead of
Auth::login($user, false);
we can use also
auth()->loginUsingId($Admin_id);
Other solutions for protected routes:
To pass the middleware Authorization, be sure to set the correct headers required, so that request is processed normally (e.g. Authorization bearer token
). Read more about Laravel Bearer Token and how to set it here.
Thank you for reading! If you enjoyed this article:
Clap it ! Share it! Follow Me in Medium!
Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.
Have a very wonderful day!