使用 Routing.yml 的 Symfony 路由
profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Profile:profile }
requirements:
id: \d+
methods: [get, delete]
如果你決定使用 Routing.yml 而不是 Annotations 你可以更好地檢視所有路由並更容易搜尋和查詢。
你可以在 Routing.yml 和 Annotations 之間進行選擇。你可以將兩者用於不同的路線,但這不是最佳解決方案。
註釋 @Route()
等價於:
class ProfileController extends Controller
{
/**
* @Route("/profile/{id}", name="profile_user_profile", requirements={"id": "\d+"})
* @Method("GET", "DELETE")
*/
public function profileAction($id)
{
if (!$id) {
throw $this->createNotFoundException('User not found.');
}
return $this->render('::Profile/profile.html.twig', array('id' => $id));
}
}