Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Controller
  5. How to define Controllers as Services
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Using Annotation Routing

How to define Controllers as Services

Edit this page

Warning: You are browsing the documentation for Symfony 2.1, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to define Controllers as Services

In the book, you've learned how easily a controller can be used when it extends the base Controller class. While this works fine, controllers can also be specified as services.

To refer to a controller that's defined as a service, use the single colon (:) notation. For example, suppose you've defined a service called my_controller and you want to forward to a method called indexAction() inside the service:

1
$this->forward('my_controller:indexAction', array('foo' => $bar));

You need to use the same notation when defining the route _controller value:

1
2
3
my_controller:
    pattern:   /
    defaults:  { _controller: my_controller:indexAction }

To use a controller in this way, it must be defined in the service container configuration. For more information, see the Service Container chapter.

When using a controller defined as a service, it will most likely not extend the base Controller class. Instead of relying on its shortcut methods, you'll interact directly with the services that you need. Fortunately, this is usually pretty easy and the base Controller class itself is a great source on how to perform many common tasks.

Note

Specifying a controller as a service takes a little bit more work. The primary advantage is that the entire controller or any services passed to the controller can be modified via the service container configuration. This is especially useful when developing an open-source bundle or any bundle that will be used in many different projects. So, even if you don't specify your controllers as services, you'll likely see this done in some open-source Symfony2 bundles.

Using Annotation Routing

When using annotations to setup routing when using a controller defined as a service, you need to specify your service as follows:

1
2
3
4
5
6
7
/**
 * @Route("/blog", service="my_bundle.annot_controller")
 * @Cache(expires="tomorrow")
 */
class AnnotController extends Controller
{
}

In this example, my_bundle.annot_controller should be the id of the AnnotController instance defined in the service container. This is documented in the @Routing chapter.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
The life jacket for your team and your project

The life jacket for your team and your project

Symfony Code Performance Profiling

Symfony Code Performance Profiling

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Slava Belokurski, a Symfony contributor

Thanks Slava Belokurski (@slavchoo) for being a Symfony contributor

1 commit • 2 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia