使用 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');
相关文章