How to get request type (master/sub) in Symfony2 controller?

前端 未结 3 873
梦如初夏
梦如初夏 2021-02-19 17:23

Is there possible get request type in controller? How?

3条回答
  •  别跟我提以往
    2021-02-19 17:51

    I was looking for this myself, and it seems it is just passed around, so there doesn't seem to be one single place that knows what it is.

    My thought for solving this would be to create a simple kernel.request listener that just adds an attribute to the request. Rough (un-tested) code below:

    public function onKernelRequest(GetResponseEvent $event)
    {
        $event->getRequest()->attributes->set('_request_type', $event->getRequestType());
    }
    

    Then in the controller you should be able to do:

    $requestType = $this->getRequest()->attributes->get('_request_type');
    

    Again this is untested. You would need to write out the full listener class and add it to the services config file, but other than that I think this will work.

提交回复
热议问题