diff --git a/controller/service.rst b/controller/service.rst index 8c92aa02c31..e298148a256 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -12,12 +12,14 @@ injection like any other normal service. Referencing your Service from Routing ------------------------------------- -Registering your controller as a service is great, but you also need to make sure -that your routing references the service properly, so that Symfony knows to use it. +Registering your controller as a service is the first step, but you also need to +update your routing config to reference the service properly, so that Symfony +knows to use it. +Use the ``service_id::method_name`` syntax to refer to the controller method. If the service id is the fully-qualified class name (FQCN) of your controller, -you're done! You can use the normal ``App\Controller\HelloController::index`` -syntax in your routing and it will find your service. +as Symfony recommends, then you can use something like: +``App\Controller\HelloController::index``: .. configuration-block:: @@ -30,7 +32,7 @@ syntax in your routing and it will find your service. // ... /** - * @Route(service="app.hello_controller") + * @Route(service="App\Controller\HelloController::index") */ class HelloController { @@ -42,7 +44,7 @@ syntax in your routing and it will find your service. # config/routes.yaml hello: path: /hello - defaults: { _controller: app.hello_controller:indexAction } + defaults: { _controller: App\Controller\HelloController::index } .. code-block:: xml @@ -54,7 +56,7 @@ syntax in your routing and it will find your service. http://symfony.com/schema/routing/routing-1.0.xsd"> - app.hello_controller:indexAction + App\Controller\HelloController::index @@ -63,14 +65,9 @@ syntax in your routing and it will find your service. // config/routes.php $collection->add('hello', new Route('/hello', array( - '_controller' => 'app.hello_controller:indexAction', + '_controller' => 'App\Controller\HelloController::index', ))); -.. note:: - - When using the ``service_id:method_name`` syntax, the method name must - end with the ``Action`` suffix. - .. _controller-service-invoke: Invokable Controllers @@ -78,7 +75,7 @@ Invokable Controllers If your controller implements the ``__invoke()`` method - popular with the Action-Domain-Response (ADR) pattern, you can simply refer to the service id -(``App\Controller\HelloController`` or ``app.hello_controller`` for example). +(``App\Controller\HelloController`` for example). Alternatives to base Controller Methods ---------------------------------------