thezombieguy / laravel-internal-request
A Laravel package to handle internal route requests
Installs: 1 076
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- illuminate/contracts: ^10.0|^11.0
- illuminate/http: ^10.0|^11.0
- illuminate/routing: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^8.0
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- spatie/phpunit-watcher: ^1.0
- squizlabs/php_codesniffer: ^3.7
README
Easily request data from internal routes within your Laravel application without making external HTTP calls.
Sometimes, it's easier and more efficient to request data from an existing internal route rather than making an external HTTP call back to your application. This package provides a clean and simple interface to make internal route requests, saving overhead and improving performance.
Requirements
- PHP >= 8.1
- Laravel 10.x or 11.x
Installation
You can install this package via Composer. It's recommended for scenarios where you want to avoid external HTTP calls and instead make requests to your own application internally.
composer require thezombieguy/laravel-internal-request
Usage
To use the internal request service, resolve it from the Laravel service container:
use TheZombieGuy\InternalRequest\Services\InternalRequestService; $service = app(InternalRequestService::class);
To call an internal route, use the $service->request() method:
$response = $service->request('valid.route.name');
By default, the service will send a get request to the internal route and return a standard response, similar to an external HTTP call.
Passing Parameters
You can also pass HTTP methods, query parameters, URL parameters, and additional headers. Let's assume you have a route called test.route
with the URL structure /test/{id}
:
use TheZombieGuy\InternalRequest\Services\InternalRequestService; $service = app(InternalRequestService::class); $urlParams = ['id' => 123]; $queryParams = ['foo' => 'bar']; $headers = ['x-test-header' => 'something fun']; $response = $service->request( 'test.route', 'GET', $urlParams, $queryParams, $headers, $bodyParams, // for post put or update methods );
This will call the route /test/123
with the url params and headers.
Hooking Into the Request Lifecycle
You can hook into the request lifecycle to execute custom logic before or after the internal request is made. This is useful when calling the internal route from services like Livewire, where you may need to avoid altering the original request object.
Example: Resetting Request State
In your AppServiceProvider
, you can bind the afterRequest
callback to restore the original request object after making an internal request:
class AppServiceProvider extends ServiceProvider { public function register() { // Bind the service and add the default afterRequest hook $this->app->bind(InternalRequestService::class, function () { $service = new InternalRequestService(); // Define the default afterRequest closure $originalRequest = \request(); $service->setAfterRequest(static function () use (&$originalRequest) { // Reset the request back to its original state App::instance('request', $originalRequest); }); return $service; }); } }
And then just call your service in the code wherever you require.
use TheZombieGuy\InternalRequest\Services\InternalRequestService; $service = \app(InternalRequestService::class); $response = $service->request('your.route');
The setAfterRequest()
hook will restore the original request after the internal request has been completed. You can also use setBeforeRequest()
to define logic that should execute before the internal request.
Lifecycle Events
When executing the before and after request hooks, the service will dispatch the following events:
- internal_request.before
- internal_request.after
You can listen to these events in your application to execute custom logic before or after the internal request is made.
## License This package is open-source software licensed under the [MIT License](LICENSE.md).