Symfony - 路由
路由将请求 URI 映射到特定控制器的方法。一般来说,任何 URI 都有以下三个部分 −
- 主机名段
- 路径段
- 查询段
例如,在 URI / URL 中,http://www.tutorialspoint.com/index?q=data,www.tutorialspoint.com 是主机名段,index 是路径段,q=data 是查询段。通常,路由会根据一组约束检查页面段。如果任何约束匹配,则返回一组值。其中一个主要值是控制器。
注释
注释在 Symfony 应用程序的配置中起着重要作用。注释通过在编码本身中声明配置来简化配置。注释只不过是提供有关类、方法和属性的元信息。路由广泛使用注释。尽管路由可以在没有注释的情况下完成,但注释在很大程度上简化了路由。
以下是示例注释。
/** * @Route("/student/home") */ public function homeAction() { // ... }
路由概念
考虑在"student"项目中创建的StudentController类。
StudentController.php
// src/AppBundle/Controller/StudentController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class StudentController extends Controller { /** * @Route("/student/home") */ public function homeAction() { // ... } /** * @Route("/student/about") */ public function aboutAction() { } }
此处,路由执行两个步骤。如果您转到 /student/home,则匹配第一个路由,然后执行 homeAction()。否则,如果您转到 /student/about,则匹配第二个路由,然后执行 aboutAction()。
添加通配符格式
假设您有一个分页的学生记录列表,其中 URL 分别为 /student/2 和 /student/3,分别对应第 2 页和第 3 页。然后,如果您想更改路由的路径,可以使用通配符格式。
示例
// src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class StudentController extends Controller { /** * @Route("/student/{page}", name = "student_about", requirements = {"page": "\d+"}) */ public function aboutAction($page) { // ... } }
此处的 \d+ 是匹配任意长度数字的正则表达式。
分配占位符
您可以在路由中分配占位符值。其定义如下。
// src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class StudentController extends Controller { /** * @Route("/student/{page}", name = "student_about", requirements = {"page": "\d+"}) */ public function aboutAction($page = 1) { // ... } }
此处,如果您访问 /student,则 student_about 路由 将匹配,并且 $page 将默认为 1。
重定向到页面
如果您想将用户重定向到另一个页面,请使用 redirectToRoute() 和 redirect() 方法。
public function homeAction() { // redirect to the "homepage" route return $this->redirectToRoute('homepage'); // redirect externally eturn $this->redirect('http://example.com/doc'); }
生成 URL
要生成 URL,请考虑路由名称 student_name 和该路由路径中使用的通配符名称 student-names。生成 URL 的完整列表定义如下。
class StudentController extends Controller { public function aboutAction($name) { // ... // /student/student-names $url = $this->generateUrl( ‘student_name’, array(‘name’ => ’student-names’) ); } }
StudentController
考虑 StudentController 类中路由的一个简单示例,如下所示。
StudentController.php
<?php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class StudentController { /** * @Route("/student/home") */ public function homeAction() { $name = 'Student details application'; return new Response( '<html><body>Project: '.$name.'</body></html>' ); } }
现在,请求 URL"http://localhost:8000/student/home",它会产生以下结果。
同样,您也可以为 aboutAction() 创建另一个路由。