From 49f589cb547a1062b064c80a60bfed0e14db8d2e Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Wed, 12 Apr 2017 19:20:47 +0200 Subject: [PATCH 1/3] Document FQCN named controllers --- controller/service.rst | 46 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index 0c861ed872c..fa2eb3af491 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -66,14 +66,14 @@ Then you can define it as a service as follows: # app/config/services.yml services: - app.hello_controller: + AppBundle\Controller\HelloController: class: AppBundle\Controller\HelloController .. code-block:: xml - + .. code-block:: php @@ -81,21 +81,24 @@ Then you can define it as a service as follows: // app/config/services.php use AppBundle\Controller\HelloController; - $container->register('app.hello_controller', HelloController::class); + $container->register(HelloController::class, HelloController::class); Referring to the Service ------------------------ -To refer to a controller that's defined as a service, use the single colon (:) -notation. For example, to forward to the ``indexAction()`` method of the service -defined above with the id ``app.hello_controller``:: +If the fully-qualified class name (FQCN) of your controller is also the id of +your service then you can refer to your controller using the usual notations. +For example, to forward to the ``indexAction()`` method of the service +defined above with the id ``AppBundle\Controller\HelloController``:: - $this->forward('app.hello_controller:indexAction', array('name' => $name)); + $this->forward('AppBundle:Hello:index', array('name' => $name)); -.. note:: +To refer to a controller that's defined as a service whose ID is not your +controller fully-qualified class name (FQCN), use the single colon (:) +notation. For example, to forward to the ``indexAction()`` method of a service +defined with the id ``app.hello_controller``:: - You cannot drop the ``Action`` part of the method name when using this - syntax. + $this->forward('app.hello_controller:indexAction', array('name' => $name)); You can also route to the service by using the same notation when defining the route ``_controller`` value: @@ -123,17 +126,24 @@ the route ``_controller`` value: '_controller' => 'app.hello_controller:indexAction', ))); +.. note:: + + You cannot drop the ``Action`` part of the method name when using this + syntax. + .. tip:: You can also use annotations to configure routing using a controller defined as a service. Make sure you specify the service ID in the - ``@Route`` annotation. See the `FrameworkExtraBundle documentation`_ for - details. + ``@Route`` annotation if your service ID is not your controller + fully-qualified class name (FQCN). See the + `FrameworkExtraBundle documentation`_ for details. .. tip:: If your controller implements the ``__invoke()`` method, you can simply - refer to the service id (``app.hello_controller``). + refer to the service id (``AppBundle\Controller\HelloController`` or + ``app.hello_controller`` for example). Alternatives to base Controller Methods --------------------------------------- @@ -209,7 +219,7 @@ argument: # app/config/services.yml services: - app.hello_controller: + AppBundle\Controller\HelloController: class: AppBundle\Controller\HelloController arguments: ['@templating'] @@ -217,7 +227,7 @@ argument: - + @@ -229,10 +239,8 @@ argument: use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; - $container->setDefinition('app.hello_controller', new Definition( - HelloController::class, - array(new Reference('templating')) - )); + $container->register(HelloController::class, HelloController::class) + ->addArgument(new Reference('templating')); Rather than fetching the ``templating`` service from the container, you can inject *only* the exact service(s) that you need directly into the controller. From 588333abfd3f07199032cec2ef779759192e3f15 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Mon, 1 May 2017 11:40:27 +0200 Subject: [PATCH 2/3] [3.3] Document FQCN named controllers --- controller/service.rst | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index fa2eb3af491..d8ea4ba816b 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -66,14 +66,13 @@ Then you can define it as a service as follows: # app/config/services.yml services: - AppBundle\Controller\HelloController: - class: AppBundle\Controller\HelloController + AppBundle\Controller\HelloController: ~ .. code-block:: xml - + .. code-block:: php @@ -81,7 +80,7 @@ Then you can define it as a service as follows: // app/config/services.php use AppBundle\Controller\HelloController; - $container->register(HelloController::class, HelloController::class); + $container->register(HelloController::class); Referring to the Service ------------------------ @@ -220,14 +219,13 @@ argument: # app/config/services.yml services: AppBundle\Controller\HelloController: - class: AppBundle\Controller\HelloController arguments: ['@templating'] .. code-block:: xml - + @@ -236,10 +234,9 @@ argument: // app/config/services.php use AppBundle\Controller\HelloController; - use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; - $container->register(HelloController::class, HelloController::class) + $container->register(HelloController::class) ->addArgument(new Reference('templating')); Rather than fetching the ``templating`` service from the container, you can From 5fb20031685967b40a8027ec4dc2108cef83065f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 3 May 2017 09:01:55 +0200 Subject: [PATCH 3/3] Minor reword --- controller/service.rst | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index d8ea4ba816b..63af920ab11 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -85,17 +85,15 @@ Then you can define it as a service as follows: Referring to the Service ------------------------ -If the fully-qualified class name (FQCN) of your controller is also the id of -your service then you can refer to your controller using the usual notations. -For example, to forward to the ``indexAction()`` method of the service -defined above with the id ``AppBundle\Controller\HelloController``:: +If the service id is the fully-qualified class name (FQCN) of your controller, +you can keep using the usual notation. For example, to forward to the +``indexAction()`` method of the above ``AppBundle\Controller\HelloController`` +service:: $this->forward('AppBundle:Hello:index', array('name' => $name)); -To refer to a controller that's defined as a service whose ID is not your -controller fully-qualified class name (FQCN), use the single colon (:) -notation. For example, to forward to the ``indexAction()`` method of a service -defined with the id ``app.hello_controller``:: +Otherwise, use the single colon (``:``) notation. For example, to forward to the +``indexAction()`` method of a service with the id ``app.hello_controller``:: $this->forward('app.hello_controller:indexAction', array('name' => $name)); @@ -127,8 +125,8 @@ the route ``_controller`` value: .. note:: - You cannot drop the ``Action`` part of the method name when using this - syntax. + You cannot drop the ``Action`` part of the method name when using the + single colon notation. .. tip::