$security 的默认值应该是多少?

What should be the default value for $security?

我正在研究这个方法:

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,
            )
        )
    );
}

但是我在调​​用时遇到了这个错误:

Controller "GroupDCA\PDOneBundle\Controller\LoginController::loginAction()" requires that you provide a value for the "$security" argument (because there is no default value or because there is a non optional argument after this one).

该参数的默认值应该是多少?我的使用方式对吗?

首先,您不需要将 $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,
        )
    );
}

您可以从获取这段代码的位置阅读更多内容:How to Build a Traditional Login Form