使用 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));
}
}