symfony2路由上的前缀变量

时间:2014-01-27 18:08:42

标签: symfony routing

我的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}

那么我如何从控制器内部获取$ religion变量?

5 个答案:

答案 0 :(得分:2)

你试过这个吗?

//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 }

让我知道它是否有效。

答案 1 :(得分:1)

您需要的可能解决方案是:

文件:src / Acme / CoreBundle / Twig / 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 / 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]

设置路由选项:

文件:app / config / routing.yml

_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

答案 2 :(得分:0)

您可以通过这种方式解决此问题 -

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

答案 3 :(得分:0)

可能是为时已晚,但它可以帮助其他人 我和Symfony以及FOSUserBundle面临同样的问题 在FOSUserBundle中,配置文件链接为:

  

site_url / profile /
site_url / profile / edit
  SITE_URL /简档/改变密码

我有一个管理员和信息中心面板,我希望这些链接位于管理面板

  

site_url / admin / profile /
site_url / admin / profile / edit
  SITE_URL /管理/简档/改变密码

并在用户信息中心

  

site_url / dashboard / profile /
site_url / dashboard / profile / edit
  SITE_URL /仪表板/简档/改变密码

为了得到这种行为,没有EventListener和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>

或您想要的任何其他前缀 现在,如果您使用更改密码链接,则会出现此错误

  

在渲染模板期间抛出了异常(“Some   缺少必需参数(“前缀”)来生成URL   路线“fos_user_change_password”。“)in   FOSUserBundle:ChangePassword:changePassword_content.html.twig at line   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">

这会给你这个错误

  

缺少一些必需参数(“前缀”)来生成URL   路线“fos_user_profile_show”。

我找到的最终解决方案是覆盖控制器
只需复制应用程序控制器文件夹中的孔控制,更改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">

答案 4 :(得分:0)

你可以得到这样的宗教价值:

use Symfony\Component\HttpFoundation\Request;

...

public function showAction(Request $request){
   $yourReligionIs = $request->get('religion');
}
相关问题