0

私はこの方法に取り組んでいます:

public function loginAction(Request $request, Security $security)
{
    $session = $request->getSession();
    $session->remove('admin_project_id');

    if ($security->has(Security::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
    } else {
        $error = $session->get(Security::AUTHENTICATION_ERROR);
        $session->remove(Security::AUTHENTICATION_ERROR);
    }

    return $this->render('PDOneBundle:Login:index.html.twig',
        array_merge($this->defaultViewParams(), array(
            'last_username' => $session->get(Security::LAST_USERNAME),
            'error'         => $error,
            'include_sidebar' => false,
            )
        )
    );
}

しかし、呼び出されたときにこのエラーが発生しました:

コントローラー「GroupDCA\PDOneBundle\Controller\LoginController::loginAction()」では、「$security」引数に値を指定する必要があります (デフォルト値がないか、この引数の後にオプションではない引数があるため)。

その引数のデフォルト値は何ですか? 私が使っている方法は正しいですか?

4

1 に答える 1

4

まず第一に、引数として $security を渡す必要はありません。セキュリティを使用する行では、実際に $request にアクセスし、その属性でエラーを確認する必要があります。

次に、あなたが質問にsymfony 2.7のタグを付けているのを見ました。バージョン @2.6 以降、新しい短いログイン方法が導入されました。

public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'security/login.html.twig',
        array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        )
    );
}

このコードが取得された場所から詳細を読むことができます:従来のログインフォームを構築する方法

于 2015-04-14T19:50:13.657 に答える