Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions controller/forwarding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ and calls the defined controller. The ``forward()`` method returns the
:class:`Symfony\\Component\\HttpFoundation\\Response` object that is returned
from *that* controller::

public function index(string $name): Response
public function __invoke(string $name): Response
{
$response = $this->forward('App\Controller\OtherController::fancy', [
$response = $this->forward('OtherController::class', [
'name' => $name,
'color' => 'green',
]);
Expand All @@ -26,10 +26,16 @@ from *that* controller::
The array passed to the method becomes the arguments for the resulting controller.
The target controller method might look something like this::

public function fancy(string $name, string $color): Response
public function __invoke(string $name, string $color): Response
{
// ... create and return a Response object
}

Like when creating a controller for a route, the order of the arguments of the
``fancy()`` method doesn't matter: the matching is done by name.
target method doesn't matter: the matching is done by name.

.. note::

Twig's ``app.current_route`` will be empty after such a ``->forward()``.
But you can set its value manually by adding an array key ``_route`` to
`forward()```s second argument.