doushuzd3033 2013-12-19 11:49
浏览 29
已采纳

Symfony2通过从当前URL替换路由器模式变量来创建URL(如何编辑当前URL?)

I want to replace the emailtemplates router to contactpoints from the following URL

  http://localhost/app_dev.php/config/workflow/configset-72/partner_site/emailtemplates

My routing pattern is: /config/workflow/configset-{configSetId}/partner_site/{steps}

I am currently using following code to generate a URL. But in this way I have to redefine the confgSetId, so, is there any other way to replace only "steps" (emailtemplates with contactpoints) route pattern?

        $routes = new RouteCollection();
        $routes->add('deploy_config', new Route('/config/workflow/configset-{configSetId}/partner_site/{steps}'));
        $context = new RequestContext();
        $context->fromRequest($this->getRequest());
        $urlGenerator = new UrlGenerator($routes, $context);
        $r = $urlGenerator->generate('deploy_config', array('configSetId' => $this->getConfigSet()->getId(), 'steps' => 'contactpoints'));

If I ask it simply I want to know: How to edit current URL inside a controller in Symfony2?

  • 写回答

1条回答 默认 最新

  • douqiao1997 2013-12-19 12:25
    关注

    You could redirect to the new url like this assuming you are inside an action:

    return $this->redirect($r);
    

    Here is the relating part in the documentation: http://symfony.com/doc/current/book/controller.html#redirecting

    To get the current route use this:

    $currentRoute = $request->attributes->get('_route');
    $currentUrl = $this->get('router')->generate($currentRoute, array(), true);
    

    Therefore you need to implement the Request in the action:

    use Symfony\Component\HttpFoundation\Request;
    ...
    function someAction(Request $request){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办