Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony 2 equivalent for url_for() function in symfony 1

In Symfony 1 we can access an action in template page as for example url_for('modulename/actionname') without writing anything in routing.yml.

how is this possible in Symfony2?,that is if i have to access more than one action in a twig without specifying in routing.this is useful while using ajax.

Thanks in advance

like image 736
Bhanu Krishnan Avatar asked Nov 04 '11 07:11

Bhanu Krishnan


1 Answers

If I understand your question correctly, you're asking how you can generate a url by passing a module name and action name, instead of a route name. Is that right?

I don't think this is possible in Symfony2. If you take a look at the generate method in Symfony\Component\Routing\Generator\UrlGenerator you'll see that it expects a route's name as the first parameter. Also, Symfony2 doesn't natively support the generic routes that symfony 1 does (shown below for reference).

default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*

Without these generic routes, you can't simply access /myModule/myAction without actually defining a route for it. And don't forget that Symfony2 now uses bundles, which would complicate this further.

So for whatever actions you want to access, you'll need to write routes for them.

In order to actually generate the URLs...
- From a controller: $this->generateUrl($routeName);
- From a PHP template: $view['router']->generate($routeName);
- From a Twig template: {{ path('_routeName') }} or {{ url('_routeName') }} for an absolute URL

like image 172
Steven Mercatante Avatar answered Nov 15 '22 05:11

Steven Mercatante