Symfony-2>登录和带占位符的注销路由 [英] Symfony-2 > login & logout routes with placeholders
问题描述
我正在使用Symfony-2来实现我的应用程序.
我需要我的登录和注销路由具有一些占位符,但是我无法在 routing.yml 和 security.yml 文件中对其进行很好的定义.
我想要类似的东西:
www.mysite.com/{client_slug}/panel
I am using Symfony-2 to implement my application.
I need my login and logout routes to have some placeholders, but I don't manage to define it well in routing.yml and security.yml files.
I want to have something like:
www.mysite.com/{client_slug}/panel
及其下的其他安全页面:
www.mysite.com/{client_slug}/panel/.*
当某人尝试导航至以下页面之一时,应将其重定向至:
www.mysite.com/{client_slug}/login
然后单击登出后,应将用户重定向至以下内容:
www.mysite.com/{client_slug}/goodbye
and under it other secured pages:
www.mysite.com/{client_slug}/panel/.*
When someone tries to navigate to one of these pages, he/she should be redirected to:
www.mysite.com/{client_slug}/login
and after clicking logout, user should be redirected to something like:
www.mysite.com/{client_slug}/goodbye
我尝试了几件事( http://forum.symfony -project.org/viewtopic.php?f=23&t=37809),但目前我唯一要做的就是在登录网址中显示 {client_slug} 文本:
I tried several things (http://forum.symfony-project.org/viewtopic.php?f=23&t=37809) but at the moment the only thing I achieved was that in my login url the text {client_slug} appears:
www.mysite.com/my-cliend-slug/panel
重定向到
www.mysite.com/{client_slug}/login
www.mysite.com/my-cliend-slug/panel
redirects to
www.mysite.com/{client_slug}/login
security.yml
firewalls:
main:
pattern: /.*
form_login:
check_path: /login_check
login_path: _security_login
logout:
path: /logout
target: /goodbye
security: true
anonymous: true
routing.yml
_security_login:
pattern: /{_client_slug}/login
defaults: { _controller: MyAppBackendBundle:Security:login }
_security_check:
pattern: /login_check
_security_logout:
pattern: /logout
_admin_panel:
pattern: /{_client_slug}/panel
defaults: { _controller: MyAppBackendBundle:AdminPanel:index }
有什么想法吗?
推荐答案
我遇到了完全相同的问题.我已经从这里阅读了所有内容( http://forum.symfony -project.org/viewtopic.php?f=23&t=37809 ),对我来说,它需要一些额外的代码.
I had the exact same problem. I've read everything from here (http://forum.symfony-project.org/viewtopic.php?f=23&t=37809) and for me it works with a few extra lines.
这是我的安全性.yml
Here is my security.yml
login_area:
pattern: ^/[A-Za-z0-9\-\_]+/login$
anonymous: ~
secured_area:
pattern: ^/[A-Za-z0-9\-\_]+/.*
form_login:
login_path: login
check_path: login_check
logout:
path: logout
target: /
remember_me:
key: "%secret%"
lifetime: 31536000
path: /
domain: ~
login和login_path定义:
The login and login_path definitions:
login:
pattern: /{_client}/login
defaults: { _controller: GNCApplicationBaseBundle:Security:login }
login_check:
pattern: /{_client}/login_check
我创建了一个EventListener,它将在kernel.request事件中调用:
And I made an EventListener, which will be called at the kernel.request event:
acme.system.client.listener:
class: Acme\System\ClientBundle\EventListener\ClientListener
arguments: [@router, @doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 10 }
重要属性是优先级.在Symfony文档( http://symfony.com/doc/current/reference/dic_tags.html#kernel-event-listener ),它显示RouterListener的优先级为32,防火墙的优先级为8.因此,我选择在防火墙之前调用我的自定义EventListener并设置_client -在路由器上下文中手动分配属性:
The important attribute is the priority. In the Symfony Documentation (http://symfony.com/doc/current/reference/dic_tags.html#kernel-event-listener) it shows that the RouterListener starts with a priority 32 and the Firewall at 8. So I choose to call my custom EventListener right before the Firewall and set the _client-attribute manually in the router-context:
public function onKernelRequest(GetResponseEvent $event) {
$clientIdentifier = $event->getRequest()->attributes->get('_client');
$this->router->getContext()->setParameter('_client', $clientIdentifier);
}
对我来说效果很好.
我当前正在使用Symfony 2.2.0-RC3.
I'm currently using Symfony 2.2.0-RC3.
这篇关于Symfony-2>登录和带占位符的注销路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!