Symfony2: Routing to Controller as Service with Annotations

A very quick post as I couldn’t find anything documenting this yet (I will submit this to the docs also). If you want to use the combination of annotations for routing and also make your controllers into services then you can by specifying the service id in the class route:

<?php
 
namespace ExampleBundle\Controller;
 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
//--
 
/** @Route("/example", service="example_bundle.controller.example_controller") */
class ExampleController
{
//--
}

Whether you should make controllers services is a while different matter and I am going to stay away from that can of worms (at least for today).

Related posts:

  1. Symfony2: Routing and mod_rewrite
  2. Symfony2: Controller as Service
  3. Symfony2: Moving Away From the Base Controller
  4. Symfony2: Top Down Perspective of Service Configuration
  5. Symfony2: Base Class for Common Dependencies
You can leave a response, or trackback from your own site.

4 Responses to “Symfony2: Routing to Controller as Service with Annotations”

  1. Kevin says:

    Nice, maybe create a PR to the docs?

  2. sebwebdev says:

    Nice, actions as controllers is the way to go (at least for me). Missed that topic a bit in your (otherwise great) talk at sfdaycgn. But I guess that aimed more towards beginners.

Leave a Reply

Please use [code] and [/code] around any source code/html/xml you wish to include in a comment.

Subscribe to RSS Feed Follow me on Twitter!