Routing
Routing¶
The Symfony CMF Routing component extends the Symfony core routing component to allow more flexibility. The most important difference is that the CMF Routing component can load routing information from a database.
Tip
This chapter provides background information on the Symfony CMF routing implementation. If you simply want to use routing in the CMF, read the RoutingBundle. If you want to customize how the routing works, please read on in this chapter.
Like the Symfony routing component, the CMF routing component does not require the full Symfony Framework and can be used in standalone projects as well.
At the core of the Symfony CMF Routing component is the
ChainRouter. The ChainRouter tries to match a request with each
of its registered routers, ignoring the
Symfony\Component\Routing\Exception\ResourceNotFoundException
until all routers got a chance to match. The first match wins - if no router
matched, the Symfony\Component\Routing\Exception\ResourceNotFoundException
is thrown. The default Symfony router can be added to this chain, so the
standard routing mechanism can still be used in addition to any custom routing.
Additionally, this component provides the DynamicRouter. This router is more configurable and flexible than the Symfony core router. It can be configured to load routes from a database, dynamically add information to the routes and also generate URLs from model classes.
The goal of Routing¶
Routing is the task of a framework to determine, based on the web request, what
code it has to call and which parameters to apply. The Symfony core
Symfony\Component\Routing\Matcher\RequestMatcherInterface
defines
that a router must convert a Symfony\Component\HttpFoundation\Request
into an array of routing information. In the full stack Symfony framework, the
code to call is defined in the _controller
field of the match parameters.
The framework is going to call the controller specified, matching eventual
parameters of that method by name with the other parameters found in the match
array or in the Request
object attributes field.
Note
For a good introduction to routing in the Symfony framework, please read the Routing chapter of the Symfony book.
To use this component outside of the Symfony framework context, have a look at the core Symfony Routing Component to get a fundamental understanding of the component. The CMF Routing Component just extends the basic behavior.
Installation¶
You can install this component with composer using the
symfony-cmf/routing package. If you are using the
symfony-cmf/routing-bundle
you do not need to specify the component
separately, it is required automatically.
Symfony integration¶
As mentioned before, this component was designed to use independently of the Symfony framework. However, if you wish to use it as part of your Symfony CMF project, an integration bundle is also available. Read more about the RoutingBundle in RoutingBundle in the bundles documentation.
Sections¶
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.