1-33
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Symfony2 – Franz Jordán 2011<br />
Like the {slug} placeholder before, the value matching {page} will be available inside your<br />
controller. Its value can be used to determine which set of blog posts to display for the given<br />
page.<br />
But hold on! Since placeholders are required by default, this route will no longer match on<br />
simply/blog. Instead, to see page 1 of the blog, you'd need to use the URL /blog/1! Since<br />
that's no way for a rich web app to behave, modify the route to make the {page} parameter<br />
optional. This is done by including it in the defaults collection:<br />
blog:<br />
pattern: /blog/{page}<br />
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }<br />
By adding page to the defaults key, the {page} placeholder is no longer required. The<br />
URL /blogwill match this route and the value of the page parameter will be set to 1. The<br />
URL /blog/2 will also match, giving the page parameter a value of 2. Perfect.<br />
/blog {page} = 1<br />
/blog/1 {page} = 1<br />
/blog/2 {page} = 2<br />
Adding Requirements<br />
Take a quick look at the routes that have been created so far:<br />
blog:<br />
pattern: /blog/{page}<br />
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }<br />
blog_show:<br />
pattern: /blog/{slug}<br />
defaults: { _controller: AcmeBlogBundle:Blog:show }<br />
Can you spot the problem? Notice that both routes have patterns that match URL's that look<br />
like/blog/*. The Symfony router will always choose the first matching route it finds. In other<br />
words, theblog_show route will never be matched. Instead, a URL like /blog/my-blogpost<br />
will match the first route (blog) and return a nonsense value of my-blog-post to<br />
the {page} parameter.<br />
URL<br />
route parameters<br />
/blog/2 blog {page} = 2<br />
/blog/my-blog-post blog<br />
{page} = my-blog-post<br />
The answer to the problem is to add route requirements. The routes in this example would work<br />
perfectly if the /blog/{page} pattern only matched URLs where the {page} portion is an<br />
59