如何解决控制器“ SecurityController :: loginAction”要求您为“ $ authenticationUtils”参数提供一个值
自从项目(symfony3.4)中删除fos用户捆绑包以来,我正在尝试建立登录表单
我的问题是loginAction,它需要AuthenticationUtils,但接收到空值。
我尝试将其链接到我的services.yml中,但不会让步。
任何帮助将不胜感激。
以下文件[SecurityController.PHP,services.yml,routing.tml]
<?PHP
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
public function loginAction(AuthenticationUtils $authenticationUtils)
{
// 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',[
'last_username' => $lastUsername,'error' => $error,]);
}
}
routing.yml
login:
path: /{_locale}/login
defaults: { _controller: 'AppBundle:Security:login' }
requirements:
_locale: "%languages%"
services.yml
services:
AppBundle\Controller\SecurityController:
class: 'AppBundle\Controller\SecurityController'
arguments: ['@security.authentication_utils']
希望有人有一个主意,因为我已经被困了几天了。
预先感谢
解决方法
如果您要使用自动装配,这会更容易(因为您不需要任何复杂的配置即可在操作中使用服务)-但让我们看看为什么这当前不起作用。
通过服务配置,可以为AppBundle\Controller\SecurityController
的构造函数提供参数。该类在当前状态下不包含任何构造函数,因此该类不包含对AuthenticationUtils
服务的引用。
如果您不想使用自动装配,这将有所帮助:在控制器类中添加一个构造函数,Symfony的容器将注入服务
class SecurityController extends Controller
{
private $authenticationUtils;
public function __construct(AuthenticationUtils $authenticationUtils) {
$this->authenticationUtils = $authenticationUtils;
}
public function loginAction()
{
// get the login error if there is one
$error = $this-authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $this-authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig',[
'last_username' => $lastUsername,'error' => $error,]);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。