Giter Club home page Giter Club logo

symfony-main-request-trait's Introduction

Symfony Main Request Trait

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-v7 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
    }
}

symfony-main-request-trait's People

Contributors

loevgaard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.