Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Main Request Trait Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package to your Laravel project via Composer:

    composer require setono/symfony-main-request-trait
    
  2. 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
        }
    }
    
  3. Where to Look First

    • Trait Documentation: Review the trait’s methods (getMainRequest(), push(), pop()).
    • Symfony Integration: If using Symfony’s RequestStack, ensure it’s bound in Laravel’s container (e.g., via AppServiceProvider).
    • Laravel-Specific Workarounds: Check if the trait conflicts with Laravel’s native Request handling (e.g., request() helper).

Implementation Patterns

Core Workflows

  1. 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();
        }
    }
    
  2. Mocking Requests in Tests Override the getMainRequest() method in tests to simulate requests:

    $this->app->instance(RequestStack::class, $mockRequestStack);
    
  3. Integration with Laravel’s Request Combine with Laravel’s Request facade for hybrid access:

    $request = $this->getMainRequest() ?? request();
    

Advanced Patterns

  1. 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();
    }
    
  2. Symfony-Laravel Bridge Bind Symfony’s RequestStack to Laravel’s container in AppServiceProvider:

    $this->app->singleton(RequestStack::class, function () {
        return new RequestStack();
    });
    
  3. Custom Request Handling Extend the trait to add Laravel-specific logic:

    trait LaravelRequestStackTrait
    {
        use RequestStackTrait;
    
        public function getLaravelRequest()
        {
            return $this->getMainRequest() ?? request();
        }
    }
    

Gotchas and Tips

Pitfalls

  1. No Native Laravel Integration

    • The package assumes Symfony’s RequestStack is available. Without binding it to Laravel’s container, getMainRequest() will return null.
    • Fix: Manually bind RequestStack or use Laravel’s request() as a fallback.
  2. Request Scope Conflicts

    • The trait’s push()/pop() may interfere with Laravel’s request lifecycle (e.g., in middleware).
    • Fix: Use cautiously in non-request-scoped contexts (e.g., commands, jobs).
  3. Deprecated Symfony Methods

    • The package may rely on Symfony methods deprecated in newer versions (e.g., getClientIp() vs. getClientIp() alternatives).
    • Fix: Check Symfony’s upgrading guide for compatibility.

Debugging Tips

  1. Null Requests If getMainRequest() returns null, verify:

    • RequestStack is bound to the container.
    • The stack isn’t empty (use count() to check).
  2. 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();
    }
    

Extension Points

  1. 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);
    }
    
  2. Laravel-Specific Request Methods Add Laravel-specific helpers to the trait:

    public function getLaravelPath()
    {
        return $this->getMainRequest()->path();
    }
    
  3. Request Stack Initialization Pre-populate the stack with Laravel’s request in the AppServiceProvider:

    $this->app->afterResolving(RequestStack::class, function ($stack) {
        $stack->push(request());
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calliostro/spotify-bundle
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle