Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Forward Requests to another Controller

How to Forward Requests to another Controller

Though not very common, you can also forward to another controller internally with the forward() method. Instead of redirecting the user's browser, this makes an "internal" sub-request and calls the defined controller. The forward() method returns the Response object that is returned from that controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public function index($name)
{
    $response = $this->forward('App\Controller\OtherController::fancy', array(
        'name'  => $name,
        'color' => 'green',
    ));

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

    return $response;
}

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($name, $color)
{
    // ... create and return a Response object
}

Just 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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.