Symfony:内部转发与渲染控制器

Symfony:内部转发与渲染控制器,symfony,php-7,symfony3.x,Symfony,Php 7,Symfony3.x,我在简单控制器中有简单操作: public function _targetAction(RequestStack $requestStack) { $request = $requestStack->getMasterRequest(); // ... } 有两种说法。第一: // The same or other controller public function topAction(Request $request) { // forward to Si

我在简单控制器中有简单操作:

public function _targetAction(RequestStack $requestStack)
{
    $request = $requestStack->getMasterRequest();
    // ...
}
有两种说法。第一:

// The same or other controller
public function topAction(Request $request)
{
    // forward to SimpleController:_target
    return $this->forward('AppBundle:Simple:_target');
}
第二个来自细枝子请求:

// SimpleController
public function topAction(Request $request)
{
    // render
    return $this->render('AppBundle:Simple:top.html.twig');
}     

// top.html.twig
{{ render(controller('AppBundle:Simple:_target')) }}
我如何确定使用此方法访问SimpleController::\u targetAction的方式:

public function _targetAction(RequestStack $requestStack)
{
    // what can i do here to uniquely identify current way
    // Note: $requestStack->getParentRequest() is not null in both cases
}

在我看来,如果您需要根据调用类型执行不同的代码,您应该考虑为每个操作创建单独的路由

如果您真的想使用相同的方法,我最好的办法是在路由上添加一个参数来标识请求

    /**
     *
     * @Route("/target/{from}", name="_target")
     */
    public function _targetAction($from)
    {
        if($from == 'view'){
            // execute code for view call
        } else {
            // execute code for controller call
        }
    }
然后,在调用它时,根据调用方类型传递不同的参数:

小枝

控制器


不知道你想做什么。你试过->isMasterRequest吗?@emix isMasterRequest我只能调用KernelEvent,不确定这是否是一个好主意尝试让事件在控制器中运行。你尝试做的这件事根本不是一个好主意。你想做什么?@emix render controller-这不是个好主意吗?选择?我试着处理synchr。ajax请求由一个操作topAction完成,但如果一切都这么简单,我会使用isXmlHttpRequest。。。不管怎样,我已经找到了解决办法。但问题仍然没有解决。您可以使用render controller第三个参数,这是您的POST键/值{{{RenderController:Action,{parameter:value},{POST_parameter:value}}}这是一个显而易见的解决方案,这是我首先想到的。但如果只是在目标行动中?对不起,你能更好地解释你的问题吗?仅在targetAction中?我的意思是可能可以在简单的内部完成:u targetAction,使用RequestStack的一些症状,而不传递额外的参数。可能可以使用$currentRequest->Header->get'referer';但在我看来,这会使事情变得复杂。
{{ render(controller('AppBundle:Simple:_target', { 'from': 'view' })) }}
return $this->forward('_target', array('from' => 'controller'));