2015-11-10 30 views
0

我想要返回不同的響應,具體取決於調用哪個頁面的操作。Symfony2:根據請求的自定義響應

這是一個動作

/** 
* @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對象。

+2

只需使用一個參數或兩個不同的路徑進行相同的操作即可。 – chapay

回答

1

正如已經在評論中提到的那樣,您可以輕鬆使用這個參數。

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); 
    } 
} 
相關問題