Symfony2:根据请求自定义响应

Symfony2: custom response according to request

我有一个想要return不同响应的操作,具体取决于调用此操作的页面。

这是一个动作

 /**
 * @Route("/collection_create_submit", name="collection_create_submit")
 */
public function createSubmitAction(Request $request)
{
    $collection = new Collection();

    /* other code*/

    if (???){
    return $this->render('@Collection/Collection/createSubmit.html.twig', 
                          array('collection' => $collection));
    }else{
    return array('collection' => $collection);
         }
    }
}

因此,例如,如果在 list.html.twig 上调用了操作,我想呈现 createSubmit.html.twig 模板。如果它是在 show.html.twig 上调用的,我只想获取 Collection 对象。

如评论中所述,您可以轻松地为此使用参数。

public function createSubmitAction(Request $request, $render = false)
{
    $collection = new Collection();

    /* other code*/

    if ($render !== false){
        return $this->render('@Collection/Collection/createSubmit.html.twig', 
                          array('collection' => $collection));
    }
    else{
        return array('collection' => $collection);
    }
}