 
- Symfony 教程
- Symfony - 主页
- Symfony - 简介
- Symfony - 安装
- Symfony - 架构
- Symfony - 组件
- Symfony - 服务容器
- Symfony - 事件和事件监听器
- Symfony - 表达
- Symfony - 捆绑包
- 创建一个简单的 Web 应用程序
- Symfony - 控制器
- Symfony - 路由
- Symfony - 视图引擎
- Symfony - Doctrine ORM
- Symfony - 表单
- Symfony - 验证
- Symfony - 文件上传
- Symfony - Ajax 控制
- Cookie 和会话管理
- Symfony - 国际化
- Symfony - 日志记录
- Symfony - 电子邮件管理
- Symfony - 单元测试
- Symfony - 高级概念
- Symfony - REST 版
- Symfony - CMF 版
- 完整的工作示例
- Symfony 有用资源
- Symfony - 快速指南
- Symfony - 有用的资源
- Symfony - 讨论
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类。
学生控制器.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() 。
添加通配符格式
假设您有一个学生记录的分页列表,其中第 2 页和第 3 页的 URL 分别为/student/2 和 /student/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 
   \return $this->redirect('http://example.com/doc'); 
}
生成 URL
要生成 URL,请考虑该路由的路径中使用的路由名称、student_name和通配符名称、student-name 。生成 URL 的完整列表定义如下。
class StudentController extends Controller { 
   public function aboutAction($name) { 
      // ...  
      // /student/student-names 
      $url = $this->generateUrl( 
         ‘student_name’, 
         array(‘name’ =>
         ’student-names’) 
      ); 
   } 
}
学生控制器
考虑一个在 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()创建另一个路由。