Posted on 15-04-15 by Peter


Routing is everything, whenever you go to a webpage there is some kind of routing behind it telling the server how to respond. In the early days we simply had that each url had a corresponding file, i.e which evolved into that you used a index.php file with get parameters, i.e the indexfile would take care of the routing

So routing in Symfony is done in your apps routing.yml file (can be other fileformats but i’m using yaml file) and in this file you will match all possible pages and if not matched the response will be an error page. There are several different ways to write a route and below is an excerpt of a routing table

1 2 3 4 5 6 7 8 9
  path: /
  defaults: {_controller: bundlename:Default:index }
  path: /admin
  defaults: { _controller: bundlenamne:Default:admin }
  resource: “@bundlename/Controller/MyController.php"
  type: annotation

There are three different pattterns or names here and the two first are only different in paths but with different functions in the controller to handle the response. I think you remember from the first post of mine that the _controller is pointing to the source to be handled by the DefaultController and functions indexAction respectively adminAction. The third one _test is using annotation and that makes this really interesting actually. It tells us that the resource we are using is a controller named MyController and the path matching is done in this controller. You don’t actually need to have a named controller in the resource, if you omit it, all controllers will be checked for the path that is requested. When doing so you can actually skip the two first examples and just let the annotation take care of it.

  resource: “@bundlename/Controller/"
  type: annotation

So the above example is actually the only thing we need to write in a routing file and the actual routing is done in the controllers. So how is this done?
Well first thing we need to do is to add a use statement because we need the class Route.

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

So the class Route gives us a very nice way of creating path patterns to each function by using the docblock and the @Route(path). So lets say that we wanna match / path which is my _welcome above and is simply the startpage, we add to the docblock of our function a @Route(“/“). So when the request for “/“ the routing goes through all controllers and returns the first function which @Route matches the request. It doesn’t matter if there are any possible matches after that, so make sure to get the routes in a hierarchy. Now this wouldn’t be any fun if we couldn't have patterns so as you can see my full blogposts have the url in the form i.e /blog/2015-04-14/routing. The /blog is static so that part of the url won’t change but the two next they will actually change for each post so our function needs the to determine which post is requested. So in our @Route we can add parameters that are optionals by writing them in a curly brackets.

1 2 3 4
 * @Route("/blog/{date}”, defaults={“date”=150309}, name="_test")
public function myAction($date){….}

This tells us that whenever we get a call for i.e /blog/150310 the myAction should be called. As you can see we have a corresponding variable for the name in the curly brackets in our function {date} = $date and we also have that if there is no date i.e only /blog the date should get a default value, in this case 150309. That would be the same as writing myAction($date=“150309”).

This was just a scratch and beginning on this issue, I'll come back to this as this site evolves more.