6

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
Justin
  • 4,203
  • 7
  • 41
  • 58

4 Answers4

13

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.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
13

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Kyril
  • 3,056
  • 1
  • 19
  • 13
  • 1
    Yes, this works like a charm in 1.2. But I do get -- The sfLoader::loadHelpers() method is deprecated. Please use the same method from sfApplicationConfiguration. – Justin Jun 10 '09 at 02:11
1

you should use

sfLoader::loadHelpers(array('Url'));
Ales Maticic
  • 1,895
  • 3
  • 13
  • 27
0

You shouldn't use sfContext::getInstance() to retrieve the configuration.

// instead of using
sfContext::getInstance()->getConfiguration()->loadHelpers('Url');

// use 
sfApplicationConfiguration::getActive()->loadHelpers(array('Url'));
j0k
  • 22,600
  • 28
  • 79
  • 90