如何在 Symfony 4 中创建登录页面而不会出现“InvalidConfigurationException”错误?
Posted
技术标签:
【中文标题】如何在 Symfony 4 中创建登录页面而不会出现“InvalidConfigurationException”错误?【英文标题】:How can I create a login page in Symfony 4 without the error "InvalidConfigurationException"? 【发布时间】:2018-12-07 19:36:02 【问题描述】:config/packages/security.yaml:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
main:
anonymous: ~
form_login:
login_path: login
check_path: login
# ...
access_control:
- path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY
- path: ^/, roles: ROLE_ADMIN
src/Controller/SecurityController.php:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
/**
* @Route("/login", name="login")
*/
public function login(Request $request, 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', array(
'last_username' => $lastUsername,
'error' => $error,
));
src/Controller/DefaultController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends Controller
/**
* @Route("/admin")
*/
public function admin()
return new Response('<html><body>Admin page!</body></html>');
模板/安全/login.html.twig:
% extends 'base.html.twig' %
% if error %
<div> error.messageKey|trans(error.messageData, 'security') </div>
% endif %
<form action=" path('login') " method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value=" last_username " />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#
<button type="submit">login</button>
</form>
我想加载我的 /admin 或 /login 页面我收到错误:
InvalidConfigurationException 下无法识别的选项“providers” “security.encoders.App\Entity\User”
在 ArrayNode->normalizeValue(array('providers' => 数组('our_db_provider' => 数组('实体' => 数组('类' => '应用\实体\用户','属性' => '电子邮件','防火墙' => 数组('main' => 数组('匿名' => null, 'form_login' => array('login_path' => 'login', 'check_path' => 'login', 'access_control' => 数组(数组('路径' => '^/login', '角色' => 'IS_AUTHENTICATED_ANONYMOUSLY'), array('path' => '^/', 'roles' => 'ROLE_ADMIN')))))))))) 在 BaseNode.php 第 368 行
【问题讨论】:
【参考方案1】:您的 security.yaml 配置中的列表有误。你有这个:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
应该是:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
encoders
和providers
都应该是security
配置下的一级子级。
【讨论】:
谢谢,这解决了大多数错误。但是还是有这个错误:Cannot determine controller argument for "App\Controller\SecurityController::login()": the $request argument is type-hinted with the non-existent class or interface: "App\Controller\Request". Did you forget to add a use statement?
将use Symfony\Component\HttpFoundation\Response;
添加到SecurityController
类的顶部
感谢您的建议。我做到了,但仍然是同样的错误
糟糕,我的意思是use Symfony\Component\HttpFoundation\Request;
是的,我现在正在处理文档,我通过文档创建了这个登录,但我才刚刚开始,所以还有一些问题:) 但是非常感谢你的帮助!真的:)以上是关于如何在 Symfony 4 中创建登录页面而不会出现“InvalidConfigurationException”错误?的主要内容,如果未能解决你的问题,请参考以下文章