|
| 1 | +.. index:: |
| 2 | + single: Routing |
| 3 | + |
| 4 | +The Routing Component |
| 5 | +==================== |
| 6 | + |
| 7 | + The Routing Component maps a HTTP request to a set of configuration |
| 8 | + variables. |
| 9 | + |
| 10 | +Installation |
| 11 | +------------ |
| 12 | + |
| 13 | +You can install the component in many different ways: |
| 14 | + |
| 15 | +* Use the official Git repository (https://github.com/symfony/Routing.git); |
| 16 | +* Install it via PEAR (`pear.symfony.com/Routing`); |
| 17 | +* Install it via Composer (`symfony/routing` on Packagist) |
| 18 | + |
| 19 | +Usage |
| 20 | +----- |
| 21 | + |
| 22 | +In order to set up a basic routing system you need three parts: |
| 23 | + |
| 24 | +* A `:class:Symfony\\Component\\Routing\\RouteCollection`, which contains the route definitions (instances of the class `:class:Symfony\\Component\\Routing\\Route`) |
| 25 | +* A `:class:Symfony\\Component\\Routing\\RequestContext`, which has information about the request |
| 26 | +* A `:class:Symfony\\Component\\Routing\\Matcher\\UrlMatcher`, which performs the mapping of the request to a single route |
| 27 | + |
| 28 | +.. code-block:: php |
| 29 | + |
| 30 | + use Symfony\Component\Routing\Matcher\UrlMatcher; |
| 31 | + use Symfony\Component\Routing\RequestContext; |
| 32 | + use Symfony\Component\Routing\RouteCollection; |
| 33 | + use Symfony\Component\Routing\Route; |
| 34 | +
|
| 35 | + $routes = new RouteCollection(); |
| 36 | + $routes->add('route_name', new Route('/foo', array('controller' => 'MyController'))); |
| 37 | +
|
| 38 | + $context = new RequestContext(); |
| 39 | +
|
| 40 | + $matcher = new UrlMatcher($routes, $context); |
| 41 | +
|
| 42 | + $parameters = $matcher->match( '/foo' ); |
| 43 | + // array('controller' => 'MyController', '_route' => 'route_name') |
| 44 | +
|
| 45 | +You can add as many routes as you like to a |
| 46 | +`:class:Symfony\\Component\\Routing\\RouteCollection`. The first argument you have to |
| 47 | +provide to the `:method:Symfony\\Component\\Routing\\RouteCollection::add` method is |
| 48 | +the name of the Route. The constructor of the `:class:Symfony\\Component\\Routing\\Route` |
| 49 | +class expects an url path and an array of custom variables. These are returned by the |
| 50 | +Matcher if the route matches the request. |
| 51 | + |
| 52 | +If no matching route can be found a |
| 53 | +`:class:Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException` will be thrown. |
| 54 | + |
| 55 | +Additionally to the custom variables the name of the route is added with the |
| 56 | +key "_route". |
| 57 | + |
| 58 | +Defining routes |
| 59 | +~~~~~~~~ |
| 60 | + |
| 61 | +A route contains: |
| 62 | + |
| 63 | +* The path the of route, which shall be recognized by the matcher. You can use ``{placeholders}`` to match dynamic parts in the url. |
| 64 | +* The default values. These contain the values that will be returned when the request matches the route. |
| 65 | +* The requirements. These define constraints for the values of the placeholders as regular expressions. |
| 66 | +* The options. These contain internal settings for the route |
| 67 | + |
| 68 | +.. code-block:: php |
| 69 | + |
| 70 | + $route = new Route( |
| 71 | + '/archive/{month}', // path |
| 72 | + array('controller' => 'showArchive'), // default values |
| 73 | + array('month' => '[0-9]{4}-[0-9]{2}'), // requirements |
| 74 | + array() // options |
| 75 | + ); |
| 76 | + |
| 77 | + // ... |
| 78 | + |
| 79 | + $parameters = $matcher->match('/archive/2012-01'); |
| 80 | + // array('controller' => 'showArchive', 'month' => 2012-01'', '_route' => '...') |
| 81 | + |
| 82 | + $parameters = $matcher->match( '/archive/foo' ); |
| 83 | + // throws ResourceNotFoundException |
| 84 | +
|
| 85 | +Besides the regular expression constraints there are two special requirements |
| 86 | +you can define: |
| 87 | + |
| 88 | +* ``_method`` enforces a certain HTTP request method (``HEAD``, ``GET``, ``POST``, ...) |
| 89 | +* ``_scheme`` enforces a certian HTTP scheme (``http``, ``https``) |
| 90 | + |
| 91 | +.. code-block:: php |
| 92 | + |
| 93 | + // Only accepts requests to /foo with the POST method and a secure connection. |
| 94 | + $route = new Route('/foo', array('_method' => 'post', '_scheme' => 'https' )); |
| 95 | +
|
| 96 | +Using prefixes |
| 97 | +~~~~~~~~ |
| 98 | + |
| 99 | +You can add routes or other instances of |
| 100 | +`:class:Symfony\\Component\\Routing\\RouteCollection` to a collection. This way |
| 101 | +you can build a tree of routes. Additionally you can define a prefix, default |
| 102 | +requirements and default options to all routes of a subtree: |
| 103 | + |
| 104 | +.. code-block:: php |
| 105 | + |
| 106 | + $rootCollection = new RouteCollection(); |
| 107 | + |
| 108 | + $subCollection = new RouteCollection(); |
| 109 | + $subCollection->add( /*...*/ ); |
| 110 | + $subCollection->add( /*...*/ ); |
| 111 | + |
| 112 | + $rootCollection->addCollection($subCollection, '/prefix', array('_scheme' => 'https')); |
| 113 | +
|
| 114 | +Set the request parameters |
| 115 | +~~~~~~~~ |
| 116 | + |
| 117 | +The `:class:Symfony\\Component\\Routing\\RequestContext` provides information |
| 118 | +about a request. You can define all parameters of a HTTP request with this class: |
| 119 | + |
| 120 | +.. code-block:: php |
| 121 | + |
| 122 | + public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443) |
| 123 | +
|
| 124 | +Normally you can pass the values from the ``$_SERVER`` variable variable to the |
| 125 | +`:class:Symfony\\Component\\Routing\\RequestContext`. If you use Symfony you can |
| 126 | +use it's `:class:Symfony\\Component\\HttpFoundation\\Request` object to feed the |
| 127 | +`:class:Symfony\\Component\\Routing\\RequestContext` in a shortcut: |
| 128 | + |
| 129 | +.. code-block:: php |
| 130 | + |
| 131 | + use Symfony\Component\HttpFoundation\Request; |
| 132 | + |
| 133 | + $context = new RequestContext(); |
| 134 | + $context->fromRequest(Request::createFromGlobals()); |
| 135 | +
|
| 136 | +Generate a URL |
| 137 | +~~~~~~~~ |
| 138 | + |
| 139 | +While the `:class:Symfony\\Component\\Routing\\Matcher\\UrlMatcher` tries to find |
| 140 | +a route that fits the given request you can also build an URL from a certain route: |
| 141 | + |
| 142 | +.. code-block:: php |
| 143 | + |
| 144 | + use Symfony\Component\Routing\Generator\UrlGenerator; |
| 145 | +
|
| 146 | + $routes = new RouteCollection(); |
| 147 | + $routes->add('show_post', new Route('/show/{slug}')); |
| 148 | +
|
| 149 | + $context = new RequestContext(); |
| 150 | +
|
| 151 | + $generator = new UrlGenerator($routes, $context); |
| 152 | +
|
| 153 | + $url = $generator->generate('show_post', 'My_Blog_Post'); |
| 154 | + // /show/My_Blog_Post |
| 155 | +
|
| 156 | +.. note:: |
| 157 | + |
| 158 | + An absolute URL is generated if you have defined a ``_scheme`` requirement for |
| 159 | + the matched route. |
| 160 | + |
| 161 | +Load routes from a file |
| 162 | +~~~~~~~~ |
| 163 | + |
| 164 | +There is a number of loader classes. They give you the abbility to load a collection |
| 165 | +of route definitions from external files. There are: |
| 166 | + |
| 167 | +* `:class:Symfony\\Component\\Routing\\Loader\\XmlFileLoader` |
| 168 | +* `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoader` |
| 169 | +* `:class:Symfony\\Component\\Routing\\Loader\\PhpFileLoader` |
| 170 | +* and others |
| 171 | + |
| 172 | +Here is an example with `:class:Symfony\\Component\\Routing\\Loader\\YamlFileLoader`: |
| 173 | + |
| 174 | +.. code-block:: yaml |
| 175 | +
|
| 176 | + # routes.yml |
| 177 | + route1: |
| 178 | + pattern: /foo |
| 179 | + defaults: { controller: 'MyController::fooAction' } |
| 180 | +
|
| 181 | + route2: |
| 182 | + pattern: /foo/bar |
| 183 | + defaults: { controller: 'MyController::foobarAction' } |
| 184 | +
|
| 185 | +.. code-block:: php |
| 186 | + |
| 187 | + $loader = new YamlFileLoader(); |
| 188 | + $collection = $loader->load('routes.yml'); |
| 189 | +
|
| 190 | +The all-in-one Router |
| 191 | +~~~~~~~~ |
| 192 | + |
| 193 | +The `:class:Symfony\\Component\\Routing\\Router` class is a all-in-one package of |
| 194 | +the routing algorithm. The constructor expects a loader instance, a path to the |
| 195 | +main route definition and some other settings: |
| 196 | + |
| 197 | +.. code-block:: php |
| 198 | + |
| 199 | + public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, array $defaults = array()); |
| 200 | +
|
| 201 | +With the ``cache_dir`` option you can enable route caching (if you provide a |
| 202 | +path) or disable caching (if it's set to ``null``). The caching is done |
| 203 | +automatically in the background if you want to use it. A basic example of the |
| 204 | +`:class:Symfony\\Component\\Routing\\Router` class would look like: |
| 205 | + |
| 206 | +.. code-block:: php |
| 207 | + |
| 208 | + $router = new Router(new YamlFileLoader(), "routes.yml"); |
| 209 | + $router->match('/foo/bar'); |
| 210 | +
|
| 211 | +.. note:: |
| 212 | + |
| 213 | + If you use caching the Routing component will compile new classes which |
| 214 | + are saved in the ``cache_dir``. This means your script must have write |
| 215 | + permissions for that location. |
0 commit comments