亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

路由

對于任何嚴(yán)謹(jǐn)?shù)膚eb應(yīng)用程序而言美觀的URL是絕對必須的。這意味著index.php?article_id=57這類丑陋的URL要被/read/intro-to-symfony取代。

擁有靈活性是更加重要的。你將頁面的URL從/blog改為/news時需要做些什么?你需要跟蹤和更新多少鏈接以做出改變?如果你使用Symfony的路由,這就很容易了。

Symfony路由器允許你定義創(chuàng)造性的url,映射到應(yīng)用程序的不同區(qū)域。在本章結(jié)束時,你將可以做到:

  • 創(chuàng)建復(fù)雜的路由,它們將映射到控制器

  • 在模板和控制器中生成URL

  • 從Bundle中(也可以從其它地方)加載路由資源

  • 對路由除錯

路由樣例 ?

一個路由,是指一個URL路徑(path)到一個控制器(controller)的映射。例如,你想匹配一些URL:/blog/my-post/blog/all-about-symfony,并且發(fā)送路由到一個“能夠查詢和渲染那一篇博文”的控制器上。路由簡單的很:

Annotations:// src/AppBundle/Controller/BlogController.phpnamespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller{
    /**
     * @Route("/blog/{slug}", name="blog_show")
     */
    public function showAction($slug)
    {
        // ...
    }}
YAML:# app/config/routing.ymlblog_show:
    path:      /blog/{slug}
   defaults:  { _controller: AppBundle: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        http://symfony.com/schema/routing/routing-1.0.xsd">     <route id="blog_show" path="/blog/{slug}">
        <default key="_controller">AppBundle:Blog:show</default>
    </route></routes>
PHP:// app/config/routing.phpuse Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route; $collection = new RouteCollection();$collection->add('blog_show', new Route('/blog/{slug}', array(
    '_controller' => 'AppBundle:Blog:show',))); return $collection;

定義blog_show路由模式,用于匹配像/blog/*的URL,把相關(guān)參數(shù)或通配符用slug表示并傳入。對于/blog/my-blog-post這樣的URL,slug變量得到my-blog-post的值,并供你控制器使用。美觀blog_show是一個內(nèi)部名稱,他沒什么實際的意義就是一個唯一的標(biāo)識。以后,你可用他來生成一些URL。

如果你不想去使用注釋方式,因為你不喜歡他們,或者因為你不希望依賴于SensioFrameworkExtraBundle,你也可以使用YAML,XML或者PHP。在這些格式中,_controller參數(shù)是一個特殊的鍵,它告訴symfony路由指定的URL應(yīng)該執(zhí)行哪個控制器。_controller字符串稱為邏輯名。它遵循規(guī)則指向一個特定的php類和方法,AppBundle\Controller\BlogController::showAction方法。

恭喜!你剛剛創(chuàng)建了一個路由并把它連接到控制器?,F(xiàn)在,當(dāng)你訪問/blog/my-post,showAction控制器將被執(zhí)行并且$slug變量就等于my-post

Symfony路由的目標(biāo):將請求的URL映射到控制器。遵循這一目標(biāo),你將學(xué)習(xí)到各式各樣的技巧,甚至使映射大多數(shù)復(fù)雜的URL變得簡單。

路由:深入了解 ?

當(dāng)一個請求發(fā)送到你的應(yīng)用程序,它包含一個確切的“資源”的客戶端請求地址。該地址被稱為URL(或URI),它可以是/contact、/blog/read-me或其它任何東西。下面是一個HTTP請求的例子:

GET /blog/my-blog-post

symfony路由系統(tǒng)的目的是解析url,并確定調(diào)用哪個控制器。整個過程是這樣的:

  1. 由Symfony的前端控制器(如app.php)來處理請求。

  2. symfony的核心(Kernel內(nèi)核)要求路由器來檢查請求。

  3. 路由將輸入的URL匹配到一個特定的路由,并返回路由信息,其中包括要執(zhí)行的控制器信息。

  4. Symfony內(nèi)核執(zhí)行控制器并最終返回Response對象。

1466599518_84162_47822_request-flow-1.png

路由是將一個輸入URL轉(zhuǎn)換成特定的工具來執(zhí)行控制器。

創(chuàng)建路由 ?

Symfony從一個單一的路由配置文件中加載所有的路由到你的應(yīng)用程序。美觀路由配置文件通常是app/config/routing.yml,但你也可以通過應(yīng)用程序配置文件將該文件放置在任何地方(包括xml或php格式的配置文件)。

YAML:# app/config/config.yml
framework:   
# ...
router: { resource: '%kernel.root_dir%/config/routing.yml' }
XML:<!-- app/config/config.xml --><?xml version="1.0" encoding="UTF-8" ?><container xmlns="http://symfony.com/schema/dic/services"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:framework="http://symfony.com/schema/dic/symfony"    xsi:schemaLocation="http://symfony.com/schema/dic/services        http://symfony.com/schema/dic/services/services-1.0.xsd        http://symfony.com/schema/dic/symfony        http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">     <framework:config>
        <!-- ... -->
        <framework:router resource="%kernel.root_dir%/config/routing.xml" />
    </framework:config></container>
PHP:// app/config/config.php$container->loadFromExtension('framework', array(
   // ...    'router' => array(        'resource' => '%kernel.root_dir%/config/routing.php',    ),));

盡管所有的路由都可以從一個文件加載,但是通常的做法是包含額外的路由資源。為此,你要把外部的路由文件配置到主要路由文件中。具體信息可查看本章:包含外部路由資源。

基本的路由配置 ?

定義一個路由是容易的,一個典型的應(yīng)用程序也應(yīng)該有很多的路由。一個基本的路由包含兩個部分:path 匹配和defaults數(shù)組:

Annotations:// src/AppBundle/Controller/MainController.php // ...class MainController extends Controller{
    /**
     * @Route("/")
     */
    public function homepageAction()
    {
        // ...
    }}
YAML:# app/config/routing.yml_welcome:
    path:      /
    defaults:  { _controller: AppBundle:Main:homepage }
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        http://symfony.com/schema/routing/routing-1.0.xsd">     <route id="_welcome" path="/">
        <default key="_controller">AppBundle:Main:homepage</default>
    </route> </routes>
PHP:// app/config/routing.phpuse Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route; $collection = new RouteCollection();$collection->add('_welcome', new Route('/', array(
    '_controller' => 'AppBundle:Main:homepage',))); return $collection;

該路由匹配首頁(/)并將它映射到AppBundle:Main:homepage 控制器。_controller字符串被Symfony轉(zhuǎn)換成PHP函數(shù)去執(zhí)行。美觀過程在本章(控制器命名模式)中被簡短提及。

帶參數(shù)的路由 ?

路由系統(tǒng)支持很多有趣的路由寫法。許多的路由都可以包含一個或者多個“參數(shù)或通配符”占位符:

Annotations:// src/AppBundle/Controller/BlogController.php // ...class BlogController extends Controller{
    /**
     * @Route("/blog/{slug}")
     */
    public function showAction($slug)
    {
        // ...
    }}
YAML:# app/config/routing.ymlblog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AppBundle: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        http://symfony.com/schema/routing/routing-1.0.xsd">     <route id="blog_show" path="/blog/{slug}">
        <default key="_controller">AppBundle:Blog:show</default>
    </route></routes>
PHP:// app/config/routing.phpuse Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route; $collection = new RouteCollection();$collection->add('blog_show', new Route('/blog/{slug}', array(
    '_controller' => 'AppBundle:Blog:show',))); return $collection;


美觀路徑將匹配任何/blog/*的URL。更妙的是,美觀{slug}占位符將自動匹配到控制器中。換句話說,如果該URL是/blog/hello-world,控制器中$slug變量的值就是hello-world。這可以用來,匹配博客文章標(biāo)題的字符串。

然而這種方式路由將不會匹配/blog這樣的URL,因為默認(rèn)情況下,所有的占位符都是必填的。當(dāng)然這也是可以變通的,可以在defaults數(shù)組中添加占位符(參數(shù))的值來實現(xiàn)。

添加{通配符}的條件 ?

快速瀏覽一下已經(jīng)創(chuàng)建的路由:

Annotations:// src/AppBundle/Controller/BlogController.php // ...class BlogController extends Controller{
    /**
     * @Route("/blog/{page}", defaults={"page" = 1})
     */
    public function indexAction($page)
    {
        // ...
    }     /**
     * @Route("/blog/{slug}")
     */
    public function showAction($slug)
    {
        // ...
    }}
YAML:# app/config/routing.ymlblog:
    path:      /blog/{page}
    defaults:  { _controller: AppBundle:Blog:index, page: 1 }blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AppBundle: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        http://symfony.com/schema/routing/routing-1.0.xsd">     <route id="blog" path="/blog/{page}">
        <default key="_controller">AppBundle:Blog:index</default>
        <default key="page">1</default>
    </route>     <route id="blog_show" path="/blog/{slug}">
        <default key="_controller">AppBundle:Blog:show</default>
    </route></routes>
PHP:// app/config/routing.phpuse Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route; $collection = new RouteCollection();$collection->add('blog', new Route('/blog/{page}', array(
    '_controller' => 'AppBundle:Blog:index',
    'page'        => 1,))); $collection->add('blog_show', new Route('/blog/{show}', array(
    '_controller' => 'AppBundle:Blog:show',))); return $collection;


你能發(fā)現(xiàn)問題嗎?兩個路由都匹配類似/blog/*的URL。Symfony路由總是選擇它第一個匹配的(blog)路由。換句話說,該blog_show路由永遠(yuǎn)被匹配。相反,像一個/blog/my-blog-post的URL會匹配第一個(blog)路由,并返回一個my-blog-post的值給{page}參數(shù)。

URLRouteParameters
/blog/2blog_list$page = 2
/blog/my-blog-postblog_show$slug = my-blog-post

給{通配符}一個默認(rèn)值 ?

高級的路由樣例 ?

在Symfony中你可以通過創(chuàng)建一個強大的路由結(jié)構(gòu)來實現(xiàn)你所需的一切。下面是一個示例來展示路由系統(tǒng)是如何的靈活:

Annotations:// src/AppBundle/Controller/ArticleController.php // ...class ArticleController extends Controller{
    /**
     * @Route(
     *     "/articles/{_locale}/{year}/{title}.{_format}",
     *     defaults={"_format": "html"},
     *     requirements={
     *         "_locale": "en|fr",
     *         "_format": "html|rss",
     *         "year": "\d+"
     *     }
     * )
     */
    public function showAction($_locale, $year, $title)
    {
    }}
YAML:# app/config/routing.ymlarticle_show:
  path:     /articles/{_locale}/{year}/{title}.{_format}
  defaults: { _controller: AppBundle:Article:show, _format: html }
  requirements:
      _locale:  en|fr
      _format:  html|rss
      year:     \d+
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        http://symfony.com/schema/routing/routing-1.0.xsd">     <route id="article_show"        path="/articles/{_locale}/{year}/{title}.{_format}">         <default key="_controller">AppBundle:Article:show</default>
        <default key="_format">html</default>
        <requirement key="_locale">en|fr</requirement>
        <requirement key="_format">html|rss</requirement>
        <requirement key="year">\d+</requirement>     </route></routes>
PHP:// app/config/routing.phpuse Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route; $collection = new RouteCollection();$collection->add(
    'article_show',
    new Route('/articles/{_locale}/{year}/{title}.{_format}', array(
        '_controller' => 'AppBundle:Article:show',
        '_format'     => 'html',
    ), array(
        '_locale' => 'en|fr',
        '_format' => 'html|rss',
        'year'    => '\d+',
    ))); return $collection;

正如你所看到的,美觀路由只匹配一部分URL也就是滿足{_locale}為(en或者fr)和 {year}是數(shù)字的。該路由還向你展示了你可以使用一個句號來分割兩個占位符。上面路由匹配的URL如下:

  • /articles/en/2010/my-post

  • /articles/fr/2010/my-post.rss

  • /articles/en/2013/my-latest-post.html

特殊的_format路由參數(shù)

這個示例也突顯了特殊的_format路由參數(shù)。當(dāng)使用這個參數(shù)時,匹配值將成為Request對象的“request format”(請求格式)。

最終,請求格式被用在“設(shè)置響應(yīng)的Content-Type”這種地方(如一個json請求格式將轉(zhuǎn)換成application/jsonContent-Type)。它也可以在控制器中使用,根據(jù)不同的_format值去渲染不同的模板。_format參數(shù)是一種非常強大的方式,把相同的內(nèi)容以不同格式來渲染(譯注:即輸出)。

在symfony3.0之前的版本中,可以覆寫request(對象)中的格式參數(shù)(_format),通過添加名為“_format”的query參數(shù)即可(例:/foo/bar?_format=json)。濫用這種行為被認(rèn)為是很不好的實踐,而且還會令你的程序在升級到symfony3時“特別復(fù)雜”。

有時,你想要令路由中的某些部分成為“全局配置”。symfony可以利用服務(wù)容器參數(shù)來做到這一點。參考如何在路由中使用服務(wù)容器的參數(shù)以了解更多。

特殊的路由參數(shù) ?

正如你所看到的,每個路由參數(shù)或默認(rèn)值都可以作為控制器方法的參數(shù)。此外,有三個參數(shù)是特殊的:在你的應(yīng)用程序中每個都是給你的應(yīng)用增加一個獨特的功能:

  • _controller
  • 正如你所看到的,這個參數(shù)是用來決定“當(dāng)路由匹配時”要執(zhí)行哪個控制器的。
  • _format
  • 用于設(shè)置請求格式(request format。了解詳情)。
  • _locale
  • 用于設(shè)置請求的locale (了解詳情).

控制器命名模式 ?

如果你使用YAML,XML或PHP的路由配置,那么每個路由都必須有一個_controller參數(shù),用于指示當(dāng)路匹配時應(yīng)執(zhí)行哪個控制器。這個參數(shù)使用一個簡單的字符串pattern,叫做控制器邏輯名(logical controller name),Symfony用它來映射一個特定的PHP方法或類。此pattern有三個部分,用冒號隔開:

bundle:controller:action

假設(shè),一個_controller值是一個AppBundle:Blog:show那么意味著:

BundleController ClassMethod Name
AppBundleBlogControllershowAction

該控制器可能是這樣的:

// src/AppBundle/Controller/BlogController.phpnamespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class BlogController extends Controller{
    public function showAction($slug)
    {
        // ...
    }}

注意,Symfony在Blog上添加了字符串Controller作為類名(Blog=>BlogController),添加字符串Action作為方法名(show=>showAction)。

你也可以使用它的FQCN類名及方法來指定這個類:AppBundle\Controller\BlogController::showAction。但如果你遵循一些簡單的命名約定,邏輯名將更加簡潔也更加靈活。

除了使用邏輯名和FQCN類名之外,Symfony也支持第三種指定控制器的方式。這種方式只使用一個冒號分隔(如service_name:indexAction),并將控制器作為一個服務(wù)來引用(參見如何把控制器定義為服務(wù))。

路由參數(shù)和控制器參數(shù) ?

路由參數(shù)(如{slug}是非常重要的,因為它(們)都被用作控制器方法的參數(shù):

public function showAction($slug){
  // ...}

現(xiàn)實中,defaults集將參數(shù)值一起合并成一個表單數(shù)組。該數(shù)組中的每個鍵都被做為控制器的參數(shù)。

換句話說,對于控制器方法的每個參數(shù),Symfony2都會根據(jù)該名稱來查找路由參數(shù),并將其值指向到控制器作為參數(shù)。在上面的高級示例當(dāng)中,下列變量的任何組合(以任意方式)都被用作showAction()方法的參數(shù):

  • $_locale

  • $year

  • $title

  • $_format

  • $_controller

  • $_route

占位符和defaults集被合并在一起,就就算是$_controller變量也是可用的。更多細(xì)節(jié)的討論,請參見作為 控制器–把路由參數(shù)傳入控制器。

你也可以使用指定的$_route變量,它的值是被匹配的路由名。

你甚至可以在你的路由中定義額外的信息并在你的控制器中訪問它。關(guān)于更多信息請閱讀 如何從路由向控制器傳遞額外的信息

生成URL ?

路由系統(tǒng)也用于生成URL。在現(xiàn)實中,路由是一個雙向系統(tǒng):映射URL到控制器+參數(shù)以及映射路由+參數(shù)返回URL。match()generate()方法構(gòu)成了這個雙向系統(tǒng)。使用之前的blog_show的例子:

$params = $this->get('router')->match('/blog/my-blog-post');
// array(
//     'slug'        => 'my-blog-post',
//  '_controller' => 'AppBundle:Blog:show',
// ) 
$uri = $this->get('router')->generate('blog_show', array(
  'slug' => 'my-blog-post'));
// /blog/my-blog-post

要生成一個URL,你需要指定路由的名稱(如blog_show)以及任意的通配符(如slug = my-blog-post)。有個這些信息,任何URL就可以很容易的生成了:

class MainController extends Controller{
    public function showAction($slug)
    {
        // ...         $url = $this->generateUrl(
            'blog_show',
            array('slug' => 'my-blog-post')
        );
    }}

在控制器中你沒有繼承symfony的父類Controller,那么你不可以使用generateUrl()快捷方法,但你可以使用router的generate()服務(wù)方法:

$url = $this->container->get('router')->generate(
    'blog_show',
    array('slug' => 'my-blog-post'));

在即將到來的部分中,你將學(xué)會如何在模板中生成URL地址。

如果你的應(yīng)用程序前端使用的是ajax請求,你可能希望根據(jù)你的路由配置,在JavaScript中生成URL。通過使用FOSJsRoutingBundle,你就可以做到:

var url = Routing.generate(
    'blog_show',
    {"slug": 'my-blog-post'});

更多信息請閱讀這個bundle文檔。

生成帶有Query Strings的URL ?

這個generate方法采用通配符數(shù)組來生成URL。但是如果在其中添加了額外的鍵值對,他們將會被添加成Query Strings來生成一個新的URL:

$this->get('router')->generate('blog', array(
    'page' => 2,
    'category' => 'Symfony'));// /blog/2?category=Symfony

在模板里生成URL ?

在應(yīng)用程序頁面之間進行連接時,最常見的地方就是從模板中生成URL。這樣做其實和以前一樣,但是使用的是一個模板助手函數(shù):

Twig:<a href="{{ path('blog_show', {'slug': 'my-blog-post'}) }}">
  Read this blog post.
</a>
php:<a href="<?php echo $view['router']->path('blog_show', array(
    'slug' => 'my-blog-post',)) ?>">
    Read this blog post.
</a>

生成絕對的URL ?

默認(rèn)情況下,路由器會產(chǎn)生相對的URL(如/blog)。在控制器中,很簡單的把generateUrl()方法的第三參數(shù)設(shè)置成UrlGeneratorInterface::ABSOLUTE_URL即可。

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 $this->generateUrl('blog_show', array('slug' => 'my-blog-post'), UrlGeneratorInterface::ABSOLUTE_URL);
// http://www.example.com/blog/my-blog-post

在模板引擎Twig中,要使用url()函數(shù)(生成一個絕對的URL),而不是path()函數(shù)(生成一個相對的URL)。在php中,需要要在generateUrl()中傳入UrlGeneratorInterface::ABSOLUTE_URL:

Twig:<a href="{{ url('blog_show', {'slug': 'my-blog-post'}) }}">
  Read this blog post.
</a>
php:<a href="<?php echo $view['router']->url('blog_show', array(
    'slug' => 'my-blog-post',)) ?>">
    Read this blog post.
</a>

當(dāng)生成一個絕對URL鏈接時,所使用的主機自動檢測當(dāng)前使用的Request對象。當(dāng)生成從web環(huán)境外的絕對URL(例如一個控制臺命令)這是行不通的。請參見 如何從控制臺生成URL 來學(xué)習(xí)如何解決這個問題。


總結(jié) ?

路由是一個將傳入的請求之URL映射到用來處理該請求的控制器函數(shù)的系統(tǒng)。它允許你指定一個美觀的URL,并使應(yīng)用程序的功能與URL“脫鉤”。路由是一個雙向的機制,意味著它也可以用來生成URL。

Keep Going! ?

路由,核對完畢!現(xiàn)在,去解封控制器的力量。