Call Route In php

Suppose you have some named routes like this




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(''), 'GET');

Or in the case of POST request, do this:

$request = Request::create('/foo/bar', 'POST', $params);or$request = Request::create(route(''), 'POST', $params);

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);

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(''), '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(' route has been dispatched successfully');

Then run:

php artisan dispatch:foobar

The result will be:


instead of

Auth::login($user, false); 

we can use also


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.

Leave a Comment