setono/symfony-main-request-trait

A PHP trait that makes the main/master request issue work on multiple Symfony versions

Maintainers

πŸ‘ loevgaard

Package info

github.com/Setono/symfony-main-request-trait

pkg:composer/setono/symfony-main-request-trait

Statistics

Installs: 370 021

Dependents: 3

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2022-06-07 13:41 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 0278d45abb5b3cfe88de472e8b30b40f2d0d3368

  • Joachim LΓΈvgaard <joachim.woop@loevgaard.dk>

This package is auto-updated.

Last update: 2026-06-27 12:54:32 UTC


README

πŸ‘ Latest Version
πŸ‘ Software License
πŸ‘ Build Status
πŸ‘ Code Coverage
πŸ‘ Mutation testing

A convenience library for library maintainers of Symfony bundles or libraries using Symfony components. When Symfony changed the naming from master to main in multiple places this had the consequence that certain methods were deprecated in Symfony v5 and removed in v6.

This library will let you support Symfony v4-v6 and not even think about the renaming :)

Installation

composer require setono/symfony-main-request-trait

Usage

Example with the RequestStack

<?php

declare(strict_types=1);

use Setono\MainRequestTrait\MainRequestTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class YourService
{
 use MainRequestTrait;

 private RequestStack $requestStack;

 public function __construct(RequestStack $requestStack)
 {
 $this->requestStack = $requestStack;
 }

 public function action(): void
 {
 /**
 * This is how you get the main request from the RequestStack. No need to worry about master/main, just get it
 * @var Request|null $request
 */
 $request = $this->getMainRequestFromRequestStack($this->requestStack);

 // do something with the request
 }
}

Example with event subscriber

<?php

declare(strict_types=1);

use Setono\MainRequestTrait\MainRequestTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final class YourSubscriber implements EventSubscriberInterface
{
 use MainRequestTrait;

 public static function getSubscribedEvents(): array
 {
 return [
 KernelEvents::RESPONSE => 'handle'
 ];
 }

 public function handle(KernelEvent $event): void
 {
 if (!$this->isMainRequest($event)) {
 return;
 }

 // Now we know we are dealing with the main request
 }
}