【问题标题】:Symfony: detect if controller request is a sub-requestSymfony:检测控制器请求是否是子请求
【发布时间】:2017-12-30 15:47:03
【问题描述】:

使用http://symfony.com/doc/current/templating/embedding_controllers.html,目标控制器如何知道请求是来自HTTP 还是来自模板?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    您有多种方法可以做到这一点。更简单的解决方案是注入request_stack 服务,并检查getParentRequest 是否为空。如果为空,则请求来自主请求。

    use Symfony\Component\HttpFoundation\RequestStack;
    
    // ...
    
    public function indexAction(RequestStack $requestStack)
    {
    
        $isMasterRequest = $requestStack->getParentRequest() === null;
    

    【讨论】:

    • 谢谢,为什么没有$request->getMasterRequest()这样的东西?
    • RequestStack 有一个 getMasterRequest 方法。 Request 对象没有,但你可以有一个事件监听器在子请求的事件中添加一些有用的属性。
    猜你喜欢
    • 2013-08-11
    • 2023-03-07
    • 2021-06-16
    • 2015-04-30
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多