Routing in symfony 2

Like with many other frameworks based on the concept of MVC, before any controller is actually executed, Symfony2 needs to know how to handle it. The Symfony routing configuration starts within the app/config/routing_*.yml file. Depending on which environment you fire up, this version of routing is fired up. Usually, this environment file loads the main app/config/routing.yml file, which loads other files from vendors, bundles, and so on. You can, of course, directly define your own single routings here, but it’s usually recommended to keep the bundle-specific routings within the bundle itself, while using the main routing only to import resources.

, we know our app tries to import another resource file defined as @AppBundle/Resources/config/routing.yml. The @BundleName syntax is a shortcut to the bundle namespace (in our case AppBundle) but namespace can be longer if you include the vendor in your name. Let’s take a look at the bundle routing by looking into the src/AppBundle/Resources/config/routing.yml file. You will see something similar to this:

    path:     /hello/{name}
    defaults: { _controller: AppBundle:Default:index }

Here, app_homepage is our routing name. When you want to generate a URL within a view, or when you want to redirect to a URL you will need to use the routing name to let the framework know which URL you want to generate and what parameters it should expect.


If you create a reusable bundle, it is a good habit to prefix your routing to avoid conflicts, but when you create your app routing, you can actually create them as short as possible, to make it easier for you to handle and make reference to. Just make sure you are not using the same name twice, as this will overwrite one of the rules.

Parameters are enclosed within curly brackets, so in our case, we define a URL /hello/{name} with a static part, /hello/, and a dynamic part, {name}. The dynamic part can be anything; it just needs to be properly encoded into the URL.

You may also notice the controller is referenced in an unusual way, without specifying the full path within the namespace. It’s because Symfony assumes that the controller lies within the Controller directory of the selected bundle, and this allows you to use short syntax. The long one would be as follows: AppBundleControllerDefaultController::indexAction.

You can also provide default values to make your parameter optional. Let’s modify this file a bit:

    path:     /hello/{name}
    defaults: { _controller: AppBundle:Default:index, name: 'anonymous' }

We changed the name of routing to hello, and provided a default value for the name parameter. Now it’s time to look at how our code change is working. Call your project URL, You should see a Hello anonymous! welcome text. If you type, you will see a Hello john! message.


If you don’t see the code under the URL, make sure that your server is up and running and listening on this port. Go back to Chapter 1, The Symfony Framework – Installation and Configuration, if you need any more help.

We should look back at our controller code, as it needs more explanation. Let’s focus on the action method:

public function indexAction($name)
    return $this->render('AppBundle:Default:index.html.twig', 
        array('name' => $name));

There are a few rules for methods within the controller to follow:

  • Only action methods should be public
  • Controller methods should be short; if your controller is long, consider refactoring it
  • Action methods must have the Action suffix
  • Action methods should return a valid response object

To return a response, you can do the following:

  • Render a template through the helper render method
  • Create a response object

Usually, when we deal with templates, we will use the helper method, however if the response is not HTML, but JSON or URL redirection, we don’t need to rely on helper methods, creating a new object of RedirectResponse() or JsonResponse().


Note that these classes exist within the Symfony framework SymfonyComponentHttpFoundation namespace.

The render() helper accepts a few parameters. The first is a template name. When we created the project, the template name was default/index.html.twig and with the new bundle, it is called AppBundle:Default:index.html.twig. The difference is in the location of the template. The first one is located within the main app/Resources/views directory, the second one exists within our bundle’s src/AppBundle/Resources/views directory. It is recommended to have application templates within app/Resources/views, since this gives you shorter syntax. It’s totally up to you which version you choose.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest


Get the best in web dev

Join and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)