1 回答

TA貢獻1825條經(jīng)驗 獲得超6個贊
有多種方法可以解決這個問題,但我認(rèn)為最簡單的解決方案是在控制器中實現(xiàn)某種映射。
您可以輕松地將 switch case 導(dǎo)出到服務(wù)或靜態(tài)方法,以便在不同的地方使用它。
在示例中,我引入了一個新變量$templateFile以使其更加清晰。
/**
* @Route("/{page}/", name="pagina")
*/
public function pagesAction(Request $request, $page)
{
// translated page name to template mapping
switch($page){
case 'pagina_estatica':
$templateFile = 'static_page';
break;
default:
$templateFile = $page;
}
if (!$this->get('templating')->exists('default/'. $templateFile . '.html.twig')){
throw new NotFoundHttpException();
}
return $this->render('default/' . $templateFile . '.html.twig');
}
- 1 回答
- 0 關(guān)注
- 103 瀏覽
添加回答
舉報