2014-01-27 59 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">