From 3d2590505c0855dee38163dcd14e9350b922f94d 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 | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index 28836464cd0..1c5db89b49f 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). .. versionadded:: 2.6 Support for ``__invoke()`` was introduced in Symfony 2.6. @@ -212,7 +222,7 @@ argument: # app/config/services.yml services: - app.hello_controller: + AppBundle\Controller\HelloController: class: AppBundle\Controller\HelloController arguments: ['@templating'] @@ -220,7 +230,7 @@ argument: - + @@ -232,6 +242,7 @@ argument: use Symfony\Component\DependencyInjection\Reference; $container->register('app.hello_controller', HelloController::class) + $container->register(HelloController::class, HelloController::class) ->addArgument(new Reference('templating')); Rather than fetching the ``templating`` service from the container, you can From 14f89d761c59a879251b5170d6ad5bf8b715399f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 3 May 2017 09:17:20 +0200 Subject: [PATCH 2/3] Minor reword --- controller/service.rst | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index 1c5db89b49f..34ee8a9f80e 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -86,17 +86,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)); From 280a5c7d110a0881a5fa398eba277fbd0d17fd5d Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Wed, 3 May 2017 11:07:53 +0200 Subject: [PATCH 3/3] Add an example with @Route --- controller/service.rst | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/controller/service.rst b/controller/service.rst index 34ee8a9f80e..d15b61d8228 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -137,6 +137,64 @@ the route ``_controller`` value: fully-qualified class name (FQCN). See the `FrameworkExtraBundle documentation`_ for details. + For example, you could use annotations in the ``HelloController`` defined + earlier:: + + // src/AppBundle/Controller/HelloController.php + namespace AppBundle\Controller; + + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + use Symfony\Component\HttpFoundation\Response; + + class HelloController + { + /** + * @Route("/hello") + */ + public function indexAction($name) + { + // ... + } + } + + With the following routes: + + .. configuration-block:: + + .. code-block:: yaml + + # app/config/routing.yml + app: + resource: "@AppBundle/Controller/" + type: annotation + + .. code-block:: xml + + + + + + + + + + .. code-block:: php + + // app/config/routing.php + use Symfony\Component\Routing\RouteCollection; + + $collection = new RouteCollection(); + $collection->addCollection( + // second argument is the type, which is required to enable + // the annotation reader for this resource + $loader->import("@AppBundle/Controller/", "annotation") + ); + + return $collection; + .. tip:: If your controller implements the ``__invoke()`` method, you can simply