simple login form using symfony(使用 symfony 的简单登录表单)
本文介绍了使用 symfony 的简单登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的控制器
use SymfonyComponentHttpFoundationRequest;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request,AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('blog/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
这是我的 security.yml
This is my security.yml
providers:
our_db_provider:
entity:
class: AppBundle:user
property: uname
in_memory:
memory:
users:
clement:
password: $2y$12$Z2B4JTnglzaYs4z73DBh9u/hIDN/E56CCrLKIjQxP6Q7aeLb5S6LO
roles: 'ROLE_ADMIN'
admin:
password: symfony
roles: 'ROLES_ADMIN'
ryan:
password: ryan1234
roles: 'ROLES_USER'
encoders:
SymfonyComponentSecurityCoreUserUser:
algorithm: bcrypt
cost: 12
AppBundleEntityUser:
algorithm: bcrypt
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
http_basic: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
form_login:
login_path: login
check_path: login
secured_area:
pattern: ^/
provider: our_db_provider
anonymous: ~
logout: true
access_control:
# require ROLE_ADMIN for /admin*
- { path: ^/admin, roles: ROLE_ADMIN }
这是我的看法
<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>
我得到了这个错误.
Controller "AppBundleControllerSecurityController::loginAction()" 要求您为 "$authUtils" 参数提供一个值.参数可以为空且未提供空值、未提供默认值或因为在此参数之后有一个非可选参数.
Controller "AppBundleControllerSecurityController::loginAction()" requires that you provide a value for the "$authUtils" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
推荐答案
这不是您问题的正确答案,而是一个可能的解决方案
This is not the correct answer to your question but a possible fix
- 将方法签名改为
public function loginAction(Request $request)
- 在您的操作开始处添加
$authUtils = $this->get('security.authentication_utils');
这篇关于使用 symfony 的简单登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用 symfony 的简单登录表单
基础教程推荐
猜你喜欢
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01