我正在构建登录表单,如 symfony 2.1 文档中所示。我也在做同样的事情。我在简单授权中获得了成功,但是当我创建简单的登录表单时,我找不到 login_check 路由。如果我把 login_check 路由然后我得到 login_check 错误。
文档对此只字未提。我的 security.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
和 routing.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');
}
和 login.html.twig 是
{# 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>
我应该在登录 check.controller 中输入什么...如果有人在 symfony 2.1 中有完整的登录示例,请把它放在答案中。