Symfony2学习笔记之系统路由详解

漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/intro-to-symfony 来替代。

拥有灵活性更为重要,如果你要改变一个页面的URL,比如从/blog 到 /new 怎么办?

有多少链接需要你找出来并更新呢? 如果你使用Symfony的router,这种改变将变得很简单。

Symfony2 router让你定义更具创造力的URL,你可以map你的应用程序的不同区域。

创建复杂的路由并map到controllers并可以在模板和controllers内部生成URLs

从bundles(或者其他任何地方)加载路由资源

调试你的路由

路由活动

一个路径是一个从URL 模式到一个controller的绑定。

比如假设你想匹配任何像 /blog/my-post 或者 /blog/all-about-symfony的路径并把它们发送到一个controller在那里可以查找并渲染blog实体。

该路径很简单:

YAML格式:

# app/config/routing.yml blog_show: pattern: /blog/{slug} defaults: {_controller: AcmeBlogBundle:Blog:show }

XML格式:

<!-- app/config/routing.xml --> <?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing "> <route pattern="/blog/{slug}"> <default key="_controller">AcmeBlogBundle:Blog:show</default> </route> </routes>

PHP代码格式:

// app/config/routing.php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('blog_show', new Route('/blog/{slug}', array( '_controller' => 'AcmeBlogBundle:Blog:show', )));

blog_show路径定义了一个URL模式,它像/blog/* 这里的通配符被命名为slug。对于URL/blog/my-blog-post,slug变量会得到值 my-blog-post。
_controller参数是一个特定的键,它告诉Symfogy当一个URL匹配这个路径时哪个controller将要被执行。
_controller字符串被称为逻辑名。它的值会按照特定的模式来指定具体的PHP类和方法。

// src/Acme/BlogBundle/Controller/BlogController.php namespace Acme\BlogBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class BlogController extends Controller { public function showAction($slug) { $blog = // use the $slug variable to query the database return $this->render('AcmeBlogBundle:Blog:show.html.twig', array( 'blog' => $blog, )); } }

现在当你再访问/blog/my-post 时,showAction controller将被执行并且$slug变量的值为my-post

Symfogy2 的路由器目标:映射一个请求的URL到controller。

路由:内部的秘密

当一个请求发送到应用程序时,它包含一个客户端想要获取资源的地址。这个地址叫做URL或者URI。可能是/contact,/blog/read-me或者其它样式。

GET /blog/my-blog-post

Symfony2 路由系统的目标是解析这些URL并决定哪个controller应该被执行来回复该请求。

整个路由过程可以分为:

1.请求被Symfony2的前端控制器(app.php)处理。
2.Symfony2核心(kernel)要求路由器检查请求。
3.路由器匹配接收到的URL到一个特定的路径并返回有关信息,包括应该被执行的controller。
4.Symfony2核心执行该controller,该controller最终会返回一个Response对象。

路由器层就是一个把接收到的URL转换为要执行的特定controller的工具。

创建路由

Symfony会从一个单独的路由配置文件中加载你应用程序的所有路由。该文件通常为 app/config/routing.yml。 它可以被配置成包括XML或者PHP文件等文件。

YAML格式:

# app/config/config.yml framework: # ... router: { resource: "%kernel.root_dir%/config/routing.yml" }

XML格式:

<!-- app/config/config.xml --> <framework:config ...> <!-- ... --> <framework:router resource="%kernel.root_dir%/config/routing.xml" /> </framework:config>

PHP代码格式:

// app/config/config.php $container->loadFromExtension('framework', array( // ... 'router' => array('resource' => '%kernel.root_dir%/config/routing.php'), ));

基础路由配置

定义一个路由很简单,通常一个应用程序拥有很多路由。一个基础路由是由两部分组成:pattern部分和defaults数组部分。
比如:

YAML格式:

_welcome: pattern: / defaults: { _controller: AcmeDemoBundle:Main:homepage }

XML格式:

<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing "> <route pattern="https://www.jb51.net/"> <default key="_controller">AcmeDemoBundle:Main:homepage</default> </route> </routes>

PHP代码格式:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5dbfa7f115af39aeeeaccce94cb82ce8.html