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 www.example.com/index.php?blog_no=3 to www.example.com/routing_with_symfony.
Symfony provides the functionality for defining creative URLs and mapping these URLs to your application.

routing

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

 acme_enquiry:
      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
  app:
      path:  /subscription
      defaults: { _controller: AcmeBlogBundle:Static:subscription }

and if you want to define multiple routing.yml files

 # app/config/routing.yml
  acme_data:
      resource: "@AcmeDataBundle/Resources/config/routing.yml"
      host: "example.in"
      prefix:   /
  acme_blog:
      resource: "@AcmeBlogBundle/Resources/config/routing.yml"
      host:   "subdomain.example.in"
      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 example.in domain, your AcmeDataBundle/Resources/config/routing.yml file will be used as the routing file.
and if you hit any URL with subdomain.example.in domain, your AcmeBlogBundle/Resources/config/routing.yml file will be used as the routing file.

Routing Example

 acme_blog_listing:
      path:  /blogs
      defaults: { _controller: AcmeBlogBundle:Default:first }
  acme_blog_detail:
      path:  /{arg1}
      defaults: { _controller: AcmeBlogBundle:Render:index }
  acme_blog:
      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}

 acme_blog_listing:
    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

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

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

 api_post_show:
      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.

Conclusion

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.

Comments


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