3

/adminユーザーが識別されたときに、管理者とメンバーをにリダイレクトしたいが/member、ホームページにアクセスしたい ( /)。

コントローラーは次のようになります。

public function indexAction()
{
    if ($this->get('security.context')->isGranted('ROLE_ADMIN'))
    {
        return new RedirectResponse($this->generateUrl('app_admin_homepage'));
    }
    else if ($this->get('security.context')->isGranted('ROLE_USER'))
    {
        return new RedirectResponse($this->generateUrl('app_member_homepage'));
    }
    return $this->forward('AppHomeBundle:Default:home');
}

ユーザーがログインしている場合、問題なく動作します。しかし、そうでない場合、私の i18n スイッチは私に素敵な例外を与えます:

マージ フィルターは、"AppHomeBundle:Default:home.html.twig" の配列またはハッシュでのみ機能します。

クラッシュする行:

{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'fr'})) }}

を見るとapp.request.get('_route_params')、 と同様に空app.request.get('_route')です。

もちろん、 に置き換えることで問題を解決できreturn $this->forward('AppHomeBundle:Default:home');ますreturn $this->homeAction();が、要点がわかりません。

内部要求がユーザー要求を上書きしていませんか?

注:私は使用していますSymfony version 2.2.1 - app/dev/debug

編集

Symfony のソース コードを見ると、 を使用するforwardと、サブリクエストが作成され、同じスコープに含まれなくなります。

/**
 * Forwards the request to another controller.
 *
 * @param string $controller The controller name (a string like BlogBundle:Post:index)
 * @param array  $path       An array of path parameters
 * @param array  $query      An array of query parameters
 *
 * @return Response A Response instance
 */
public function forward($controller, array $path = array(), array $query = array())
{
    $path['_controller'] = $controller;
    $subRequest = $this->container->get('request')->duplicate($query, null, $path);

    return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}

Symfony2 のスコープのドキュメントを見ると、リクエスト自体がスコープである理由とその処理方法について説明されています。しかし、転送時にサブリクエストが作成される理由については説明していません。

さらにグーグルで調べてみると、イベントリスナーが表示され、サブリクエストを処理できることがわかりました(詳細)。サブリクエストタイプについてはわかりましたが、これでもユーザーリクエストが削除された理由を説明できません。

私の質問は次のようになります。

転送時にユーザー要求が削除され、コピーされないのはなぜですか?

4

1 に答える 1

1

そのため、コントローラーのアクションはロジックの一部として分離されています。この関数は、お互いについて何も知りません。私の答えは - 単一のアクションは、特定の要求の種類を処理します (たとえば、特定の uri prarams を使用)。SF2 ドキュメントから ( http://symfony.com/doc/current/book/controller.html#requests-controller-response-lifecycle ):

2 Router はリクエストから情報 (URI など) を読み取り、その情報に一致するルートを見つけ、そのルートから _controller パラメータを読み取ります。

3一致したルートのコントローラーが実行され、コントローラー内のコードが Response オブジェクトを作成して返します。

パスに対するリクエストで、このルートを処理するアクション(/たとえば、私は(例)を使用することを意味します:indexAction()fancyAction()fancyAction()

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

代わりは:

public function fancyAction()
{
    $name = $this->getRequest()->get('name');
    $color = $this->getRequest()->get('color');
    // ... create and return a Response object
}

sf2 ドキュメントの例:

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

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

    return $response;
}

注意してくださいfurther modify the response

リクエストオブジェクトが本当に必要な場合は、次を試すことができます:

public function indexAction()
{
    // prepare $request for fancyAction

    $response = $this->forward('AcmeHelloBundle:Hello:fancy', array('request'  => $request));

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

    return $response;
}

public function fancyAction(Request $request)
{
    // use $request
}
于 2013-05-06T15:48:08.770 に答える