我有一个大问题,请帮助我.所以我想创建一个只使用以下用户的登录系统:
providers: in_memory: memory: users:
所以我的全球路由/app/config/routing.yml
:
app_admin: resource: "@AppAdminBundle/Resources/config/routing.yml" prefix: /admin
我在AdminBundle中的路由:
app_admin_homepage: path: / defaults: { _controller: AppAdminBundle:Login:index } login: path: /login defaults: { _controller: AppAdminBundle:Login:login } login_check: path: /login_check
我的LoginController:
render('AppAdminBundle:Member:login.html.twig'); } public function loginAction() { $request = $this->get('request'); $session = $this->get('session'); 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('AppAdminBundle:Member:login.html.twig', array( 'last_username' => $session->get(SecurityContext::LAST_USERNAME), 'error' => $error, )); } }
我的login.html.twig:
而我的security.yml:
security: role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_FMTI: ROLE_FMTI ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] access_control: - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin, role: IS_AUTHENTICATED_ANONYMOUSLY } firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false secured_area: pattern: ^/admin anonymous: ~ form_login: login_path: /admin/login check_path: /admin/login_check access_control: - { path: ^/admin, roles: ROLE_ADMIN } providers: in_memory: memory: users: admin: { password: 111111, roles: 'ROLE_FMTI' } encoders: Symfony\Component\Security\Core\User\User: plaintext
我试图访问时: /admin/login
我收到此错误: You have requested a non-existent service "request". Did you mean one of these: "router.request_context", "request_stack", "monolog.logger.request", "data_collector.request"?
请你帮助我好吗 ?提前致谢,对不起我的英语
而不是这个
$this->get('request')
使用
$this->get('request_stack')->getCurrentRequest()
这是因为您试图在控制器中获得不存在的服务.在symfony控制器中,您可以在方法调用中获取请求:
在Logincontroller中,添加以下use语句:
use Symfony\Component\HttpFoundation\Request;
并在您的函数声明中
public function loginAction(Request $request) { // you can directly use $request .... }
您现在可以删除尝试获取不存在服务的行:
// remove this line $request = $this->get('request');
此外,我不确定你会得到一个get('session')会话.根据Symfony的文档,您应该执行以下操作:
$session = $request->getSession();
请参阅http://symfony.com/doc/current/book/controller.html#managing-the-session