2012-07-10 52 views
2

我設立路由,允許以下路徑:Symfony2的:使用前綴與登錄路由

/client_a/dashboard/ 
/client_b/dashboard/ 

我用我app/config/routing.yml前綴,看起來像這樣:

AcmeMainBundle: 
    resource: "@AcmeMainBundle/Resources/config/routing.yml" 
    prefix: /{client} 

問題我碰到的是,登錄路由似乎與前綴有問題。下面是我在/Resources/config/routing.yml條目,我將其導入到主路由文件:

acme_login: 
    pattern: /login/ 
    defaults: {_controller: AcmeMainBundle:Main:login } 

acme_login_check: 
    pattern: /login_check 
    # defaults: This is not required since the Firewall will handle this 

acme_logout: 
    pattern: /logout/ 
    # defaults: This is not required since the Firewall will handle this 

登錄頁面顯示正常,但用戶提交的登錄頁面後,Symfony的拋出一個錯誤,指出:

Unable to find the controller for path "/client_a/login_check". Maybe you forgot to add the matching route in your routing configuration? 

在我看來,Symfony2在內部安全路由方面遇到了困難,並在routing.yml中使用了前綴。

任何方法來克服這個問題?

注意:解決此問題的一種方法是更改​​我的routing.yml文件中的所有路由以包含{client}參數。唯一的問題是,這是一個非常廣泛的應用程序與大量的路線。使用前綴工作奇蹟,除了登錄期間的安全處理。

感謝,

JB

回答

0

經過相當多的研究和嘗試不同的方面之後,在路由中使用動態前綴似乎沒有好的解決方案。

問題似乎是安全包在路由發生之前執行其代碼,所以來自前綴的路由屬性在security.yml中不可用,而是靜態路徑。這本身就是導致問題的原因,因爲Symfony隨後抱怨缺少屬性等。

所以,答案是,與安全實現相結合,上述是不可能的,至少不是我發現的方式。

0

我想你可能需要在那些路由定義控制器defaults: {_controller: AcmeMainBundle:Main:login }。它在默認情況下不需要它們,因爲防火牆在路由之前捕獲它。但是在這種情況下可能需要匹配前綴。

+0

是的,想過重寫控制器中的默認安全層處理。雖然有很多需要深入研究,我希望依靠Symfony自己的內置功能。如果我指定'login_check'的默認控制器(請注意,登錄動作看起來工作正常),我開始得到一堆其他錯誤,因爲缺少東西。 – jbsound 2012-07-10 11:44:43

-1

你需要的可能的解決方案,將是:

文件:SRC /阿克米/ CoreBundle /嫩枝/ PathExtension.php

<?php 

namespace Acme\CoreBundle\Twig\Extension; 

use Symfony\Bundle\FrameworkBundle\Routing\Router; 
use Symfony\Component\HttpKernel\Event\GetResponseEvent; 
use Symfony\Component\HttpKernel\HttpKernel; 

class PathExtension extends \Twig_Extension 
{ 

    private $request; 
    private $router; 

    public function __construct(Router $router) { 
     $this->router = $router; 
    } 

    public function onKernelRequest(GetResponseEvent $event) { 
     if ($event->getRequestType() === HttpKernel::MASTER_REQUEST) { 
      $this->request = $event->getRequest(); 
     } 
    } 

    public function getFunctions() 
    { 
     return array(
      'path_route' => new \Twig_Function_Method($this, 'getPath') 
     ); 
    } 

    public function getPath($name, $parameters = array()) 
    { 
     $parameters = array_merge($parameters, [ 
      'client' => $this->request->get('client'), 
     ]); 

     return $this->router->generate($name, $parameters, false); 
    } 

    public function getName() 
    { 
     return 'twig_path_route_extension'; 
    } 

} 

配置爲服務:

文件:SRC/Acme的/CoreBundle/Resources/config/services.yml

services: 
    twig.path_route_extension: 
     class: Acme\CoreBundle\Twig\PathExtension 
     tags: 
      - { name: twig.extension } 
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } 
     arguments: [@router] 

設置你的路由選項:

File:app/config/routing。陽明海運

_acme_client: 
    resource: "@AcmeCoreBundle/Resources/config/routing.yml" 
    prefix: /{client}/ 

然後你就可以在模板和routing.yml中使用它,爲前:

_acme_client_dashboard: 
    path:  /dashboard 
    defaults: { _controller: AcmeCoreBundle:System:dashboard } 

然後,在你的模板,你可以使用:

<a href="{{ path_route('_acme_client_dashboard') }}"> 
    Link to Dashboard. 
</a> 

參考:

Pass path parameters automatically

http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route