setono/symfony-main-request-trait
Tiny PHP trait for Symfony apps that provides easy access to the main HTTP request from the RequestStack, helping services and listeners consistently retrieve the current master request without repeating boilerplate.
Installation Add the package to your Laravel project via Composer:
composer require setono/symfony-main-request-trait
First Use Case
Use the trait in a service or controller to access Symfony’s RequestStack (if integrated) or mock the main request:
use Setono\Symfony\Component\HttpFoundation\RequestStackTrait;
class MyService
{
use RequestStackTrait;
public function getCurrentRequest()
{
return $this->getMainRequest(); // Returns the main request
}
}
Where to Look First
getMainRequest(), push(), pop()).RequestStack, ensure it’s bound in Laravel’s container (e.g., via AppServiceProvider).Request handling (e.g., request() helper).Request Access in Non-Controller Classes Use the trait to fetch the current request in services, middleware, or commands:
class OrderService
{
use RequestStackTrait;
public function getClientIp()
{
return $this->getMainRequest()->getClientIp();
}
}
Mocking Requests in Tests
Override the getMainRequest() method in tests to simulate requests:
$this->app->instance(RequestStack::class, $mockRequestStack);
Integration with Laravel’s Request
Combine with Laravel’s Request facade for hybrid access:
$request = $this->getMainRequest() ?? request();
Request Stack Management Push/pop requests for nested contexts (e.g., sub-requests in middleware):
$this->push(Request::createFromGlobals());
try {
// Process sub-request
} finally {
$this->pop();
}
Symfony-Laravel Bridge
Bind Symfony’s RequestStack to Laravel’s container in AppServiceProvider:
$this->app->singleton(RequestStack::class, function () {
return new RequestStack();
});
Custom Request Handling Extend the trait to add Laravel-specific logic:
trait LaravelRequestStackTrait
{
use RequestStackTrait;
public function getLaravelRequest()
{
return $this->getMainRequest() ?? request();
}
}
No Native Laravel Integration
RequestStack is available. Without binding it to Laravel’s container, getMainRequest() will return null.RequestStack or use Laravel’s request() as a fallback.Request Scope Conflicts
push()/pop() may interfere with Laravel’s request lifecycle (e.g., in middleware).Deprecated Symfony Methods
getClientIp() vs. getClientIp() alternatives).Null Requests
If getMainRequest() returns null, verify:
RequestStack is bound to the container.count() to check).Request Stack Corruption
Unbalanced push()/pop() calls can corrupt the stack. Use a finally block to ensure cleanup:
try {
$this->push($request);
// ...
} finally {
$this->pop();
}
Custom Request Classes Override the trait to support custom request classes:
protected function createRequest(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
return new CustomRequest($query, $request, $attributes, $cookies, $files, $server, $content);
}
Laravel-Specific Request Methods Add Laravel-specific helpers to the trait:
public function getLaravelPath()
{
return $this->getMainRequest()->path();
}
Request Stack Initialization
Pre-populate the stack with Laravel’s request in the AppServiceProvider:
$this->app->afterResolving(RequestStack::class, function ($stack) {
$stack->push(request());
});
How can I help you explore Laravel packages today?