![]() |
|
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.
Comments on this snippet
Why don't you add this method to myActions.class.php and use myActions as the default factories action class? You could overload the
forward()method and decide on execution if it's a route (1 param) or if it's a module/action combination (2 param) - and in your actions you still can call$this->forward()...Do not forget to add a "case(4)" to mangage script name if you have it in URI
Nice shot, thx
Although I haven't tested it, i think you can actually pass parameters to the action. Just get the
sfRequest'sParameterHolder, clear it and calladdorsetas often as needed. Then execute theforward(). To be gentle, catch the sfStopException which is thrown byforward(), restore the original parameters and rethrow the exception. For restoring the parameters, you can either stored the values in theParameterHolderwith a callgetAllprior to clearing, or you abuse thesfWebRequest->initialize()(but take care of the request's AttributeHolder).