![]() |
|
Snippets |
|
Here is a forward method wrapper that accepts a routing label. Since forward does not do parameters neither does this. Though it would be nice.
public static function fooForward(&$sfa, $route) { $url = sfContext::getInstance()->getController()->genUrl($route); $routing = explode("/", $url, 3); switch (count($routing)) { case (3): $sfa->forward($routing[1],$routing[2]); break; case (2): $sfa->forward($routing[1], 'index'); break; default: error_log(sprintf("Error in fooForward for routing label %s", $route)); break; } }
Call it in an action:
public function handleError() { fooTools::fooForward($this, '@someLabel'); }
If there where parameters in the label the $url would have them. Though they can not be used in this case.
Let's take an example. You want to have the user culture in front of each of your URL.
So, your routing configuration look like something like that:
page: url: /:culture/:page params: ... article: url: /:culture/:year/:month/:day/:slug params: ... ...
This is the easy part. But now, every time you call url_for() or link_to() you have to pass the culture parameter.
For such situations, there is a better way. The routing has a special configuration parameter named sf_routing_defaults that you can set with default values:
sfConfig::set('sf_routing_defaults', array('culture' => 'en'));
And now, the culture parameter will automatically added to the parameters that you pass to the url_for() or link_to() helpers.
You want to get the module, action and parameters associated to a given url, pretty much as the routing system does.
First you will have to remove the part of the url which is not symfony specific. That part typically looks like yoursite.com/path/to/symfony. Once you've done that execute the following code:
$r = new sfRouting(); $r->setRoutes(sfRouting::getInstance()->getRoutes()); $params = $r->parse($myUrl); $module = $params['module']; $action = $params['action'];
Now the module and action associated to this url are as above in $module and $action and the parameters are the remaining elements of the array $params.