[英]Symfony2 : login_check and annotation
在我的symfony项目中,我想使用身份验证。 所以我在security.yml文件中写了这个
firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false default: anonymous: ~ login_firewall: pattern: ^/login$ anonymous: ~ secured_area: pattern: ^/ anonymous: ~ form_login: login_path: /login check_path: /login_check post_only: true always_use_default_target_path: true default_target_path: / use_referer: false username_parameter: username password_parameter: password intention: authenticate logout: path: /logout target: /
好的,现在文档说我需要在routing.yml中添加以下内容:
login: pattern: /login defaults: { _controller: AcmeSecurityBundle:Security:login } login_check: pattern: /login_check
但是我正在使用注释,所以我不使用路由文件。 相反,我有这个:
/** * @Route("/login") * @Template() */ 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 array( // last username entered by the user 'last_username' => $session->get(SecurityContext::LAST_USERNAME), 'error' => $error, ); }
如果我什么也不做,则我的登录页面有错误:
"Unable to generate a URL for the named route "login_check" as such route does not exist."
如果我在控制器中添加一个响应为空的操作,则没有任何结果。
我究竟做错了什么 ?
login_check
或您指定的任何内容都是系统定义的路由,而不是操作。 这意味着您不能/不能/不会在控制器中手动定义它。 ;)
我本人使用annotations
来进行路由,但是,您将以一种或另一种方式使用routing.yml
来导入您的控制器,从而从它们内部添加注释的路由。
因此,继续,只需在routing.yml
定义该路由即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.