Skip to content

Commit

Permalink
Moving forwarding section all the way to the bottom
Browse files Browse the repository at this point in the history
I could find very little real use-cases for this.
  • Loading branch information
weaverryan committed Nov 24, 2014
1 parent 0754efc commit 6ef10db
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -515,49 +515,6 @@ The Symfony templating engine is explained in great detail in the
// puts the content into a Response object for convenience
$content = $templating->renderResponse('Hello/index.html.twig', array('name' => $name));

.. index::
single: Controller; Forwarding

Forwarding
~~~~~~~~~~

You can also forward to another controller internally with the
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
method. Instead of redirecting the user's browser, it makes an internal sub-request,
and calls the controller. The ``forward()`` method returns the ``Response``
object that's returned from *that* controller::

public function indexAction($name)
{
$response = $this->forward('AppBundle:Something:fancy', array(
'name' => $name,
'color' => 'green',
));

// ... further modify the response or return it directly

return $response;
}

Notice that the ``forward()`` method uses a special string representation
of the controller (see :ref:`controller-string-syntax`). In this case, the
target controller function will be ``SomethingController::fancyAction()``
inside the ``AppBundle``. The array passed to the method becomes the arguments
on the resulting controller. This same idea is used when embedding controllers
into templates (see :ref:`templating-embedding-controller`). The target
controller method would look something like this::

public function fancyAction($name, $color)
{
// ... create and return a Response object
}

Just like when creating a controller for a route, the order of the arguments of
``fancyAction`` doesn't matter. Symfony matches the index key names (e.g.
``name``) with the method argument names (e.g. ``$name``). If you change the
order of the arguments, Symfony will still pass the correct value to each
variable.

.. index::
single: Controller; Accessing services

Expand Down Expand Up @@ -808,6 +765,49 @@ and template are needed).

Use it! See :doc:`/cookbook/templating/render_without_controller`.

.. index::
single: Controller; Forwarding

Forwarding to Another Controller
--------------------------------

Though not very common, you can also forward to another controller internally
with the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
method. Instead of redirecting the user's browser, it makes an internal sub-request,
and calls the controller. The ``forward()`` method returns the ``Response``
object that's returned from *that* controller::

public function indexAction($name)
{
$response = $this->forward('AppBundle:Something:fancy', array(
'name' => $name,
'color' => 'green',
));

// ... further modify the response or return it directly

return $response;
}

Notice that the ``forward()`` method uses a special string representation
of the controller (see :ref:`controller-string-syntax`). In this case, the
target controller function will be ``SomethingController::fancyAction()``
inside the ``AppBundle``. The array passed to the method becomes the arguments
on the resulting controller. This same idea is used when embedding controllers
into templates (see :ref:`templating-embedding-controller`). The target
controller method would look something like this::

public function fancyAction($name, $color)
{
// ... create and return a Response object
}

Just like when creating a controller for a route, the order of the arguments of
``fancyAction`` doesn't matter. Symfony matches the index key names (e.g.
``name``) with the method argument names (e.g. ``$name``). If you change the
order of the arguments, Symfony will still pass the correct value to each
variable.

Final Thoughts
--------------

Expand Down

0 comments on commit 6ef10db

Please sign in to comment.