我正在构建symfony 2.1文档中所示的登录表单。我也在做同样的事情。我在简单的授权中获得了成功,但当我创建简单的登录表单时,我得到login_check路由未找到。如果我设置login_check路由,就会出现login_check错误。
文档中对此只字未提。我的安全。Yml是
# app/config/security.yml
security:
providers:
in_memory:
memory:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
firewalls:
login:
pattern: ^/login$
security: false
secured_area:
pattern: ^/admin
anonymous: false
form_login:
login_path: /login
check_path: /login_check
always_use_default_target_path: false
default_target_path: /admin/content/index
target_path_parameter: _target_path
use_referer: false
username_parameter: _username
password_parameter: _password
csrf_parameter: login[_token]
logout:
path: /admin/logout
target: /login
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
encoders:
Symfony'Component'Security'Core'User'User: plaintext
和路由。Yml是
login:
pattern: /login
defaults: { _controller: AcmeTaskBundle:Default:login}
login_check:
pattern: /login_check
defaults: { _controller: AcmeTaskBundle:Default:loginCheck}
logout:
pattern: /admin/logout
defaults: { _controller: AcmeTaskBundle:Default:logout}
content_index:
pattern: /admin/content/index
defaults: { _controller: AcmeTaskBundle:Default:index }
我的控制器登录动作是
namespace Acme'TaskBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Component'HttpFoundation'Request;
use Acme'TaskBundle'Entity'Product;
use Acme'TaskBundle'Entity'CmsContentMst;
use Acme'TaskBundle'Entity'CmsSectionsMst;
// use Acme'TaskBundle'Entity'ProductType;
use Symfony'Component'HttpFoundation'Response;
use Acme'TaskBundle'Form'Type'AddContent;
use Symfony'Component'Security'Core'SecurityContext;
class DefaultController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
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(
'AcmeTaskBundle:Default:login.html.twig',
array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
)
);
}
public function panelAction(Request $request)
{
}
public function loginCheckAction()
{
return new Response('true');
}
and login.html.twig is
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" 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>
我应该在login check.controller…如果有人有完整的登录例子在symfony 2.1 PLZ把它的答案。
您不必为login_check创建一个控制器。Symfony已经在默认情况下定义了它的函数。我想你不会想弄乱它的。如果你想在登录后执行操作,你可以在服务等上定义它。
这可能有帮助Symfony2:成功登录事件后,执行一组