Sitemap

Using Annotation Routing

1 min readNov 6, 2016

--

If you’d like to use annotation for routing, there are just two steps to use it :)

First, you should add

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route 

in your controller so that @Route() can be used in annotation. Remember that, in order to use everything starting with ‘@’ in an annotation, their corresponding classes must be used!

Get Fatih Yavuz’s stories in your inbox

Join Medium for free to get updates from this writer.

Second, the routes must be imported from your controller to be activated. now, go to app/config/routing.yml file and add the following

whatever: 
resource: @YourAwesomeBundle/Controller/YourController.php
type: annotation

To be honest, I don’t know what “whatever:” part is used for at the beginning of the code above. If you know, I’d like to hear that :)

Yay! Now you are ready to use awesome @Route() *method in your annotation! (* I called it method but I’m not sure if it is correct. Again, if you know what it is, please let me know! )

/**
* @Route("/edit/{id}", name="blog_home", defaults={"id" = 1}, requirements={"id" = "\d+"}, )
*/
public function indexAction()
{
// ...
}

Bonus: If you’d like to check whether your routes are applied succesfully, you can run the following command.

php app/console debug:route

--

--