21.10.2015 Views

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 />

The routes from this file are parsed and loaded in the same way as the main routing file.<br />

Prefixing Imported Routes<br />

You can also choose to provide a "prefix" for the imported routes. For example, suppose you<br />

want the acme_hello route to have a final pattern of /admin/hello/{name} instead of<br />

simply/hello/{name}:<br />

# app/config/routing.yml<br />

acme_hello:<br />

resource: "@AcmeHelloBundle/Resources/config/routing.yml"<br />

prefix: /admin<br />

The string /admin will now be prepended to the pattern of each route loaded from the new<br />

routing resource.<br />

Visualizing & Debugging Routes<br />

While adding and customizing routes, it's helpful to be able to visualize and get detailed<br />

information about your routes. A great way to see every route in your application is via<br />

the router:debug console command. Execute the command by running the following from the<br />

root of your project.<br />

php app/console router:debug<br />

The command will print a helpful list of all the configured routes in your application:<br />

homepage ANY /<br />

contact GET /contact<br />

contact_process POST /contact<br />

article_show ANY /articles/{culture}/{year}/{title}.{_format}<br />

blog ANY /blog/{page}<br />

blog_show ANY /blog/{slug}<br />

You can also get very specific information on a single route by including the route name after<br />

the command:<br />

php app/console router:debug article_show<br />

Generating URLs<br />

The routing system should also be used to generate URLs. In reality, routing is a bi-directional<br />

system: mapping the URL to a controller+parameters and a route+parameters back to a URL.<br />

Thematch() and generate() methods form this bi-directional system. Take<br />

the blog_show example route from earlier:<br />

$params = $router->match('/blog/my-blog-post');<br />

// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show')<br />

$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));<br />

// /blog/my-blog-post<br />

64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!