Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Symfony, how can I use the url_for helper in a class?

I am building a URL inside of a class and I'd like to be able to change the routing if necessary later by just changing the routing.yml file.

If I have the route:

userSignup:
  url:   /user/signup
  param: { module: user, action: signup }

How can I use the url_for('userSignup') helper in a class to dynamically create the URL?

like image 930
Justin Avatar asked Jun 10 '09 00:06

Justin


3 Answers

You don't need to use the view helpers in your action to generate a url:

$this->generateUrl("userSignup");

The method is defined in sfComponent.class.php. This is the recommended way, not the hacky workaround of loading the view helpers and using them.

like image 95
Maerlyn Avatar answered Nov 05 '22 04:11

Maerlyn


I only tried this with 1.2 so I can't speak for any previous versions...

From any of your classes:

sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));

...then you can continue with using any of the functions defined in the url helper.

like image 35
Kyril Avatar answered Nov 05 '22 04:11

Kyril


you should use

sfLoader::loadHelpers(array('Url'));
like image 1
Ales Maticic Avatar answered Nov 05 '22 02:11

Ales Maticic