create event listener

I think you come into the dark side of the dependency injection. Relaying on the request means moving to a narrower scope, because the request is created while the application is running.
But as jedi ninjas, let's try to solve this.
I would try to use a second event listener listening to kernel.request. This event gets your entity_locale_setter.listener service and sets the request manually when the kernel.requestevent occurs.
# services.yml

services:
    entity_locale_setter.listener:
        class: Giftshop\FrontBundle\Listener\EntityLocaleSetter
        tags:
            - { name: doctrine.event_listener, event: postLoad }

    kernel_request.listener:
        class: Giftshop\FrontBundle\Listener\KernelRequestListener
        arguments: [@entity_locale_setter.listener]
        tags:
            - { name: kernel.event_listener, method: onKernelRequest }
php
// KernelRequestListener.php

namespace Giftshop\FrontBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class KernelRequestListener
{

    private $entityLocaleSetter;

    public function __construct(EntityLocaleSetter $entityLocaleSetter)
    {
        $this->entityLocaleSetter = $entityLocaleSetter;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $this->entityLocaleSetter->setRequest($event->getRequest());
    }

}

评论

热门博文