Code snippets for symfony 1.x

Navigation

forward wrapper that accepts a routing label

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.

by roland cruse on 2006-09-27, tagged action  forward  handleerror  routing 

Comments on this snippet

gravatar icon
#1 Pierre Minnieur on 2006-11-07 at 05:43

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() ...

gravatar icon
#2 Gourvil Sylvain on 2007-11-20 at 05:52

Do not forget to add a "case(4)" to mangage script name if you have it in URI

Nice shot, thx

gravatar icon
#3 Jan Kunzmann on 2008-01-13 at 04:17

Although I haven't tested it, i think you can actually pass parameters to the action. Just get the sfRequest's ParameterHolder, clear it and call add or set as often as needed. Then execute the forward(). To be gentle, catch the sfStopException which is thrown by forward(), restore the original parameters and rethrow the exception. For restoring the parameters, you can either stored the values in the ParameterHolder with a call getAll prior to clearing, or you abuse the sfWebRequest->initialize() (but take care of the request's AttributeHolder).