概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:48:31 | 118 次阅读 | 评论: 0 | 来源: 网络整理

Parameters inside the defaults collection don’t necessarily have to match a placeholder in the route path. In fact, you can use the defaults array to specify extra parameters that will then be accessible as arguments to your controller:

  • YAML
    # app/config/routing.yml
    blog:
        path:      /blog/{page}
        defaults:
            _controller: AppBundle:Blog:index
            page:        1
            title:       "Hello world!"
    
  • 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>
            <default key="title">Hello world!</default>
        </route>
    </routes>
    
  • PHP
    // app/config/routing.php
    use SymfonyComponentRoutingRouteCollection;
    use SymfonyComponentRoutingRoute;
    
    $collection = new RouteCollection();
    $collection->add('blog', new Route('/blog/{page}', array(
        '_controller' => 'AppBundle:Blog:index',
        'page'        => 1,
        'title'       => 'Hello world!',
    )));
    
    return $collection;
    

Now, you can access this extra parameter in your controller:

public function indexAction($page, $title)
{
    // ...
}

As you can see, the $title variable was never defined inside the route path, but you can still access its value from inside your controller.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务