You are on page 1of 1

Creating

https://symfony.com/components/Routing

Routing
beautiful
URLs!
Fastest
PHP router
available!

4.1

a route is a map from a URL path to a controller


e.g. matched routes: Redirects
/articles/en/2000/my-post routes can be definied in config files (YAML,
/articles/fr/2000/my-post.rss XML, PHP) or in controllers (annotations) Defining 301, 302, 307,
/articles/en/2017/latest-post.html and 308 redirects:
internal route_301:
route name {placeholders}
# config/routes.yaml # ...
defaults:
article_show: # ...
path: /articles/{_locale}/{year}/{slug}.{_format} permanent: true
match a route based
on the host host: "{subdomain}.example.com" route_302:
controller: App\Controller\ArticleController::show # ...
which controller defaults:
is executed defaults: # ...
_format: html the matched value becomes permanent: false
the “request format" of
giving {placeholders} subdomain: '%subdomain%' the Request object route_307:
a default value
requirements: # ...
you can use service parameters defaults:
_locale: en|fr # ...
defining requirements if you do not want to hardcode
to {placeholders} _format: html|rss the value of the {placeholder} permanent: false
keepRequestMethod: true
year: \d+ regexp to
If no methods methods: [GET, HEAD] accept only route_308:
are specified, the numbers # ...
route will match each routing parameter
or default value is available defaults:
on all methods # ...
as an argument in the
controller method permanent: true
keepRequestMethod: true

Earlier routes always win! The order of the routes is important!

for simple
conditions Imported Routes
Inlined Configuration # config/routes.yaml
app_hello:
you can inline the route requirements resource: '@ThirdPartyBundle/Resources/config/routing.yaml'
host: "hello.example.com”
and default values in the {placeholders}. prefix: /admin
prefix:
blog_list: prefix can
Add prefix en: '/site'
# no requirements and no default value be i18n es: '/sitio'
path: /blog/{page}
to routes
# with requirements but no default value
path: /blog/{page<\d+>}
# no requirements but with a default value
path: /blog/{page?1} Trailing Slash
# no requirements but with default value = null
path: /blog/{page?} Symfony redirect (301 redirect) between URLs with and without trailing slashes
# with requirements and default value = null
in both ways (but only for GET and HEAD requests).
path: /blog/{page<.*>?}

Console
$ php bin/console debug:router visualize and get detailed information about all configured routes
$ php bin/console debug:router article_show visualize and get information of a single route
$ php bin/console router:match /blog/my-latest-post test whether a URL matches a given route

Special Routing Parameters i18n Routes


_controller determine which controller is executed when the route is matched. contact:
_format set the request format. controller: App\Controller\ContactController::send
_fragment set the fragment identifier, the optional last part of a URL that starts path:
en: /send-us-an-email
with a # character and is used to identify a portion of a document. nl: /stuur-ons-een-email
_locale set the locale on the request.

You might also like