24 May 2014
Beautiful URLs are an absolute must for any serious web application. This means leaving behind ugly URLs like index.php?article_id=57 in favor of something like /read/intro-to-symfony.
Having flexibility is even more important. What if you need to change the URL of a page from /blog to /news? How many links should you need to hunt down and update to make the change? If you’re using Symfony’s router, the change is simple.
The Symfony2 router lets you define creative URLs that you map to different areas of your application. By the end of this chapter, you’ll be able to: Create complex routes that map to controllers Generate URLs inside templates and controllers Load routing resources from bundles (or anywhere else) Debug your routes
Routing in Action¶
A route is a map from a URL path to a controller. For example, suppose you want to match any URL like /blog/my-post or /blog/all-about-symfony and send it to a controller that can look up and render that blog entry. The route is simple:
New in version 2.2: The path option was introduced in Symfony 2.2, pattern is used in older versions.
The path defined by the blog_show route acts like /blog/* where the wildcard is given the name slug. For the URL /blog/my-blog-post, the slug variable gets a value of my-blog-post, which is available for you to use in your controller (keep reading).
The _controller parameter is a special key that tells Symfony which controller should be executed when a URL matches this route. The _controller string is called the logical name. It follows a pattern that points to a specific PHP class and method:
Til next time,
Xujiajun at 07:39