0

I have this listener:

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;

class ControllerListener
{
    public function onKernelController( FilterControllerEvent $event, Request $request )
    {
        if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
            $controllers = $event->getController();
            if (is_array( $controllers )) {
                $controller = $controllers[0];
                if (is_object( $controller )) {
                    if (method_exists( $controller, 'parControl' )) {
                        $controller->parControl(
                            $controller->getRequest(),
                            $controller->getRequest()->get( 'action' ),
                            $controller->getRequest()->get( 'context' ),
                            $controller->getRequest()->get( 'page' ),
                            $controller->getRequest()->get( 'case' ),
                            $controller->getRequest()->get( 'subset' )
                        );
                    }

                    if (method_exists( $controller, 'adminProjectControl' )) {
                        // to this method I need to pass Request $request  
                        $controller->adminProjectControl($request);
                    }

                    if (method_exists( $controller, 'accessControl' )) {
                        $controller->accessControl();
                    }
                }
            }
        }
    }

    public function onKernelResponse( FilterResponseEvent $event )
    {
        Utilities::flashDebugLoginMessage();
    }
}

And it's defined as a service at services.yml:

pdone.listener.preexecute:
    class: GroupDCA\PDOneBundle\Listener\ControllerListener
    arguments: [@request]
    public: true
    scope: request
    tags:
        - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
        - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

But I should doing something wrong since I got this three errors:

ContextErrorException in ControllerListener.php line 12: Catchable Fatal Error: Argument 2 passed to GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController() must be an instance of Symfony\Component\HttpFoundation\Request, string given

ContextErrorException in ControllerListener.php line 12: Catchable Fatal Error: Argument 2 passed to GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController() must be an instance of Symfony\Component\HttpFoundation\Request, string given

LogicException in bootstrap.php.cache line 3118: No listeners of the "kernel.exception" event set a Response

So, how do I inject the Request to the listener? Can I do that? I have read also this post here but not get how to do this.

4

2 回答 2

12

FilterControllerEvent已经让您访问请求

public function onKernelController(FilterControllerEvent $event)
{
    $request = $event->getRequest();       

    // ...
}

如果您需要事件不提供请求的侦听器中的请求,请始终使用请求堆栈

use Symfony\Component\HttpFoundation\RequestStack;

class MyService
{
    private $requestStack;

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

    public function doSomething()
    {
        $request = $this->requestStack->getCurrentRequest();

        // ...
    }
}

注入请求已被弃用并将被删除。请求堆栈是要走的路。也不要使用request范围。

于 2015-04-15T11:39:05.833 回答
0

您将请求对象从服务定义传递给 ControllerListener 构造函数,而不是传递给 onKernelController 方法。

而是这样做:

protected $request;

public function __construct (Request $request) {

    $this->request = $request;
}

这应该可以解决您的问题并使请求对象可用。

于 2015-04-15T01:50:04.990 回答