How to Forward Requests to another Controller

Though not very common, you can also forward to another controller internally with the forward() method provided by the AbstractController class.

あまり一般的ではありませんが、AbstractController クラスによって提供される forward() メソッドを使用して、別のコントローラーに内部的に転送することもできます。

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:

ユーザーのブラウザをリダイレクトする代わりに、これは「内部」サブリクエストを作成し、定義されたコントローラーを呼び出します。 forward() メソッドは、そのコントローラーから返される Response オブジェクトを返します。
1
2
3
4
5
6
7
8
9
10
11
public function index($name)
{
    $response = $this->forward('App\Controller\OtherController::fancy', [
        '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:

メソッドに渡された配列は、結果のコントローラーの引数になります。ターゲット コントローラー メソッドは次のようになります。
1
2
3
4
public function fancy($name, $color)
{
    // ... 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.

ルートのコントローラーを作成する場合と同様に、fancy() メソッドの引数の順序は重要ではありません。マッチングは名前で行われます。