使用symfony的简单登录表单 [英] simple login form using symfony
本文介绍了使用symfony的简单登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的控制器
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
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,
));
}
}
这是我的安全性.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:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
AppBundle\Entity\User:
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>
我得到这个错误.
控制器"AppBundle \ Controller \ SecurityController :: loginAction()"要求您为"$ authUtils"参数提供一个值.该参数可以为空,并且没有提供空值,没有提供默认值,或者因为此参数之后有一个非可选参数.
Controller "AppBundle\Controller\SecurityController::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');
- change method signature to
public function loginAction(Request $request)
- add
$authUtils = $this->get('security.authentication_utils');
at the beginning of your action
这篇关于使用symfony的简单登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文