http://symfony.com/doc/current/book/routing.html
Routing with Placeholders¶
Of course the routing system supports much more interesting routes. Many routes will contain one or more named "wildcard" placeholders:- YAML
1 2 3
blog_show: pattern: /blog/{slug} defaults: { _controller: AcmeBlogBundle:Blog:show }
/blog/*
. Even better,
the value matching the {slug}
placeholder will be available inside your
controller by default, all placeholders are required
modify the route to make the
{page}
parameter optional.
This is done by including it in the defaults
collection:
YAML1 2 3 | blog:
pattern: /blog/{page}
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
|
blog: pattern: /blog/{page} defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 } requirements: page: \d+
Earlier Routes always Win
Suppose you have a contact form with two controllers - one for displaying the form (on a GET request) and one for processing the form when it's submitted (on a POST request). This can be accomplished with the following route configuration: YAML
1 2 3 4 5 6 7 8 9 10 11 | contact:
pattern: /contact
defaults: { _controller: AcmeDemoBundle:Main:contact }
requirements:
_method: GET
contact_process:
pattern: /contact
defaults: { _controller: AcmeDemoBundle:Main:contactProcess }
requirements:
_method: POST
|
Like the other requirements, the
_method
requirement is parsed as a regular
expressionthere are three parameters that are special: each adds a unique piece of functionality inside your application:
_controller
: As you've seen, this parameter is used to determine which controller is executed when the route is matched;_format
: Used to set the request format (read more);_locale
: Used to set the locale on the session (read more);
You can also use a special
$_route
variable, which is set to the
name of the route that was matchedAll routes are loaded via a single configuration file - usually
app/config/routing.yml
(see Creating Routes above). Commonly, however, you'll want to load routes
from other places, like a routing file that lives inside a bundle. This can
be done by "importing" that file:YAML
1 2 3 | # app/config/routing.yml
acme_hello:
resource: "@AcmeHelloBundle/Resources/config/routing.yml"
|
When importing resources from YAML, the key (e.g.
acme_hello
) is meaningless.
Just be sure that it's unique so no other lines override itPrefixing Imported Routes¶
You can also choose to provide a "prefix" for the imported routes. For example, suppose you want theacme_hello
route to have a final pattern of /admin/hello/{name}
instead of simply /hello/{name}
:YAML
1 2 3 4 | # app/config/routing.yml
acme_hello:
resource: "@AcmeHelloBundle/Resources/config/routing.yml"
prefix: /admin
|
/admin
will now be prepended to the pattern of each route
loaded from the new routing resource.A great way to see every route in your application is via the
router:debug
console command. Execute
the command by running the following from the root of your project.
1 | $ php app/console router:debug
|
You can also get very specific information on a single route by including the route name after the command:
1 | $ php app/console router:debug article_show
|
mapping the URL to a controller+parameters and a route+parameters back to a URL. The
match()
and
generate()
methods form this bi-directional
system. Take the blog_show
example route from earlier:
1 2 3 4 5 | $params = $router->match('/blog/my-blog-post');//URL to parameters
// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show')
$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));//a route+parameters back to a URL
// /blog/my-blog-post
|
If the frontend of your application uses AJAX requests, you might want to be able to generate URLs in JavaScript based on your routing configuration. By using the FOSJsRoutingBundle, you can do exactly that:
1 | var url = Routing.generate('blog_show', { "slug": 'my-blog-post'});
|
Generating Absolute URLs¶
By default, the router will generate relative URLs (e.g./blog
). To generate
an absolute URL, simply pass true
to the third argument of the generate()
method:1 2 | $router->generate('blog_show', array('slug' => 'my-blog-post'), true);
// http://www.example.com/blog/my-blog-post
|
Generating URLs with Query Strings¶
Thegenerate
method takes an array of wildcard values to generate the URI.
But if you pass extra ones, they will be added to the URI as a query string:1 2 | $router->generate('blog', array('page' => 2, 'category' => 'Symfony'));
// /blog/2?category=Symfony
|
Generating URLs from a template¶
The most common place to generate a URL is from within a template when linking between pages in your application. This is done just as before, but using a template helper function:Twig
1 2 3 | <a href="{{ url('blog_show', {'slug': 'my-blog-post'}) }}">
Read this blog post.
</a>
|
沒有留言:
張貼留言