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个回答

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){
dousi6405
dousi6405 我想将当前URL编辑为其他内容并在树枝模板中打印
接近 7 年之前 回复
立即提问
相关内容推荐