2014-01-27 57 views
6

我需要在我的Symfony2的路由上的前綴的變量,這樣我可以做這樣的事情在主路由文件的前綴:在束變量對Symfony2的路由

//app/config/routing.yml 
god: 
    resource: "@Acme/DemoBundle/Resources/config/routing.yml" 
    prefix: /god/{religion} 

,然後像這樣路由文件:

gods_route_to_heaven: 
    path: /path_to_heaven 
    defaults: { _controller: AcmeBlogBundle:God:show } 

這樣,這樣我可以訪問路徑:

/god/Christianity/path_to_heaven 
/god/Islam/path_to_heaven 
/god/Hinduism/path_to_heaven 

等。

如果我在控制檯上鍵入app/console route:debug | grep api,我會得到正確的路由/god/{religion}/path_to_heaven,所以路由正在正確生成。

但是當我嘗試把它作爲像這樣的動作功能的輸入來獲得控制器的參數:

public function showAction($religion) 

道路遭破壞,傾倒我看到它被複制的路徑:/god/{religion}/path_to_heaven/{religion}

那麼我怎麼會從控制器內部獲得$宗教變量?

回答

3

你試過嗎?

//app/config/routing.yml 
god: 
    resource: "@Acme/DemoBundle/Resources/config/routing.yml" 
    prefix: /god 

,然後像這樣在包路由文件:

gods_route_to_heaven: 
    path: /{religion}/path_to_heaven 
    defaults: { _controller: AcmeBlogBundle:God:show } 

讓我知道,如果它的工作原理。

+5

是的,這是有效的,但我試圖避免不得不把每個路口下的{宗教}佔位符放在/神下...想象一下,我有100條路線/神,我將不得不添加{宗教}佔位符每一個。如果相反,我將它添加到前綴我只會添加一次。 – Noquepoaqui

1

在此way-

god: 
    resource: "@Acme/DemoBundle/Resources/config/routing.yml" 
    prefix: /god/{_locale}/ 

沒有必要改變在這裏 -

gods_route_to_heaven: 
    path: /path_to_heaven 
    defaults: { _controller: AcmeBlogBundle:God:show } 

修改在這裏 -

public function showAction(){ 
     // below line is extra if you need 
     $post = $this->get('request')->request->all(); 
     $religion = $post['religion']; 
    } 

我不」你可以解決這個問題噸測試這個代碼,但所有的過程都證明了。如果你會看到任何問題讓我們討論。

你也可以看到這個問題,它會幫助你很Symfony2 Route global {_locale} requirements

+0

是的,這可以工作,但這不是最好的方法。如果我想使用_locale來達到真正的目的,該怎麼辦? _locale是一個特殊的參數... – Noquepoaqui

2

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

文件: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, [ 
      'religion' => $this->request->get('religion'), 
     ]); 

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

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

} 

配置爲服務:

文件:SRC /阿克米/ CoreBundle /資源/配置/ 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] 

設置你的路由選項:

文件:應用程序/配置/路由。陽明海運

_acme_religion: 
    resource: "@AcmeCoreBundle/Resources/config/routing.yml" 
    prefix: /god/{religion}/ 

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

_religion_pathheaven: 
    path:  /path_to_heaven 
    defaults: { _controller: AcmeCoreBundle:System:getpathheaven } 

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

<a href="{{ path_route('_religion_pathheaven') }}"> 
    Link Path to heaven. 
</a> 

參考:

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

0

可能是晚了,但它可以幫助其他人
我正面臨與Symfony和FOSUserBundle相同的問題。
在FOSUserBundle個人資料鏈接爲:

SITE_URL /資料/
SITE_URL /型材/編輯
SITE_URL /型材/更改密碼

我有一個管理和儀表板,和我想要的鏈接,在管理面板

SITE_URL /管理/資料/
SITE_URL /管理/配置文件/編輯
SITE_URL /管理/配置文件/更改密碼

,並在用戶面板

SITE_URL /儀表板/型材/
SITE_URL /儀表板/型材/編輯
SITE_URL /儀表板/型材/變化 - 密碼

要獲得這種行爲是非常簡單的,而不事件偵聽和TwigExtesion
這裏的解決方案:

更換

fos_user: 
    resource: "@FOSUserBundle/Resources/config/routing/all.xml" 

通過

fos_user_security: 
    resource: "@FOSUserBundle/Resources/config/routing/security.xml" 
fos_user_registration: 
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml" 
fos_user_resetting: 
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml" 
fos_user_change_password: 
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml" 
    prefix: /{prefix}/profile 
fos_user_profile: 
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml" 
    prefix: /{prefix}/profile 

在模板中使用:

<a href="{{ path('fos_user_profile_show', {'prefix': 'dashboard'}) }}">Show Profile</a> 
<a href="{{ path('fos_user_profile_edit', {'prefix': 'dashboard'}) }}">Edit Profile</a> 
<a href="{{ path('fos_user_change_password', {'prefix': 'dashboard'}) }}">Change Password</a> 

或者你想
任何其他前綴現在,如果你使用例如更改密碼鏈接你會得到這個錯誤

一個例外模板的渲染過程中被拋出(「有些 強制性參數丟失(」前綴「)來生成 路由器的URL‘fos_user_change_password’。」)在 FOSUserBundle:ChangePassword:changePassword_content.html在線 3。

你可以在模板中添加前綴像這樣:
更換

<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password"> 

通過

<form action="{{ path('fos_user_change_password', {'prefix': app.request.attributes.get('prefix') }) }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password"> 

這會給你這個錯誤

一些強制性的參數缺少(「前綴」)來生成e路由「fos_user_profile_show」的 的URL。

我找到最終的解決方案是重寫控制器
只是在你的應用程序控制器文件夾中的孔CONTROLE複製,改變namesapce並更換

if (null === $response = $event->getResponse()) { 
       $url = $this->generateUrl('fos_user_profile_show'); 
       $response = new RedirectResponse($url); 
      } 

通過

if (null === $response = $event->getResponse()) { 
       $url = $this->generateUrl('fos_user_profile_show',array('prefix'=>$request->get('prefix'))); 
       $response = new RedirectResponse($url); 
      } 

在形式上你不需要這樣做,所以它會是

<form {{ form_enctype(form) }} method="POST" class="fos_user_change_password">