Symfony Routing – How to Define Routing in Symfony

By: Sunil Kumar |  In: PHP  |  Last Updated: 2017/09/20

Beautiful URL’s are essential if you are trying to build a beautiful web application. That means you are converting your URL from something like to
Symfony provides the functionality for defining creative URLs and mapping these URLs to your application.


routing is a pathway from your URL to the controller action. Suppose you want to map your route /routing_with_symfony to your Controller, your route will be

      path:  /routing_with_symfony
      defaults: { _controller: AcmeBlogBundle:Static:Enquiry }

Here /routing_with_symfony is pointing to BlogBundle bundle -> StaticController ->ListAction method.

Formats of routing

You can configure routes either in YAML, XML or PHP. But here we are doing it in YAML format which is the most popular format.

Loading routes

Because Symfony is intended to be modular, a file is dedicated to this: routing.yml. You will find it in app/config/routing.yml Symfony loads all your routes from this file. If you want to define all routes in the same file you can do it as

 # app/config/routing.yml
      path:  /subscription
      defaults: { _controller: AcmeBlogBundle:Static:subscription }

and if you want to define multiple routing.yml files

 # app/config/routing.yml
      resource: "@AcmeDataBundle/Resources/config/routing.yml"
      host: ""
      prefix:   /
      resource: "@AcmeBlogBundle/Resources/config/routing.yml"
      host:   ""
      prefix:   /

Here resource is the path of your routing file and host is your domain for which the routing file will be used. So if you hit any URL with domain, your AcmeDataBundle/Resources/config/routing.yml file will be used as the routing file.
and if you hit any URL with domain, your AcmeBlogBundle/Resources/config/routing.yml file will be used as the routing file.

Routing Example

      path:  /blogs
      defaults: { _controller: AcmeBlogBundle:Default:first }
      path:  /{arg1}
      defaults: { _controller: AcmeBlogBundle:Render:index }
      path:  /blog/{page}
      defaults: { _controller: AcmeBlogBundle:Default:second }

Explanation :
Here acme_blog_detail, acme_blog are internal name of routes which must be unique.

  • If you hit /blogs the very first route is matched which is pointing to firstAction method of DefaultController.
  • If you hit /* the second route is matched which is pointing to indexAction method of RenderController.
  • If you hit /blogs/* the third route is matched which is pointing to secondAction method of DefaultController.

Here parameter with ‘{}’ called wildcard which can be accessed as an argument in your controller.
NOTE: It’s necessary to match wildcard and argument names.

Giving Default Value to a {wildcard}

    path:  /blogs/{page}
    defaults: { _controller: AcmeBlogBundle:Notfound:notfound,'page':1 }

In the above examples if you have defined only routing with /blogs/{arg1} and a user hit /blogs/1it will find a match but if a user hit /blog it will be a 404 because no match is found here. So if you are adding a wildcard it must have a value.
In this case, You can define a default value of wildcard
So in this case, If user hit /blog a route will match and 1 will be assigned to the page.

Earlier routes always Win

If a request matches with multiple routes then what..?
Symfony has a solution for this also. In this case, first routes win or the first route will be preferred.

Adding HTTP Method Requirements

In your routing, you can also match the method of your request ie. GET, POST etc. Suppose you want your URL to accept only post request

      path:     /api/posts/{id}
      defaults: { _controller: AppBundle:BlogApi:show }
      methods:  [POST]

If you want your URL to accept multiple methods you can allow as

      path:     /api/posts/{id}
      defaults: { _controller: AppBundle:BlogApi:show }
      methods:  [GET, POST]

If you don’t define any method, your URL will accept the request of all methods.


As you can see, managing routes with Symfony is both easy to write and easy to read and maintain. It both allows you to specify beautiful URLs and keeps the functionality of your application separated from those URLs.


Leave a Comment

Your email address will not be published.


Sunil Kumar

I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers