我正在尝试根据手册中的安全示例在symfony2中实现登录,但似乎有些错误,它没有显示任何错误或任何消息。这是控制器:
<?php
namespace WN\Bundle\UsersBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use WN\Bundle\UsersBundle\Entity\User;
use WN\Bundle\UsersBundle\Entity\UsersRepository;
class SecurityController extends Controller {
public function loginAction() {
$request = $this->getRequest();
$session = $request->getSession();
if($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR))
{
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
}
else
{
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('WNUsersBundle:Security:login.html.twig',
array('last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error
));
}
}
?>
更新
这是bundle / config / routing.yml
wn_home:
pattern: /secured
defaults: { _controller: WNHomeBundle:Home:index }
wn_users_homepage:
pattern: /hello/{name}
defaults: { _controller: WNUsersBundle:Default:index }
wn_users_register:
pattern: /register
defaults: { _controller: WNUsersBundle:Register:register }
wn_users_login:
pattern: /login
defaults: { _controller: WNUsersBundle:Security:login }
login_check:
pattern: /login_check
这是security.yml
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
WN\Bundle\UsersBundle\Entity\User: sha256
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
users:
entity: { class: WNUsersBundle:User }
firewalls:
login:
pattern: ^/login
security: false
secured_area:
pattern: ^/secured
form_login:
check_path: /login_check
login_path: /login
logout:
path: /logout
target: /login
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
这是login.html.twig
{% extends '::base.html.twig' %}
{% block content %}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="Username">Username</label>
<input type="text" name="_username" id="username" value="{{ last_username }}">
<label for="Password">Password</label>
<input type="password" name="_password" id="password">
<br />
<input type="submit" name="submit" value="Login">
</form>
{% endblock %}
现在我得到的错误是: 无法找到路径“/ login_check”的控制器。也许您忘了在路由配置中添加匹配的路由?
有谁知道如何解决这个问题?
答案 0 :(得分:0)
您必须在登录控制器中定义其他两个公共功能。你没有在其中放置任何东西,它们只是Symfony2安全控制器机制工作的基础。
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
}
希望这可以帮助你。