2
votes

I am using zend framework for my project and I have a requirement to route the path where I want it. For example: I have a path www.example.com/module/controller/action1 and I want to internally route in to www.example.com/module/controller/action2.

But the main problem is I do not want to use the function in which I have to specify the module, controller and action [$this->_forward('action2', 'controller', 'module');], simply I something like this: $this->_forward('module/controller/action2');.

Please suggest me if anybody has its solution. its urgent need of my project.

Thanks, Jitu

1
Can you explain why you want to have the url passed on? It is not possible for ZF to accept an url, because it requires the module/controller/action parameters. Otherwise, the url needs to be passed to the Router which can tell the module/controller/action. And that's a lot of overhead for a simple thing.Jurian Sluiman

1 Answers

0
votes

The controller method _forward() can accept only the action if it is in the same controller and module as the first action you are forwarding from.

So in your action1Action() method, you can simply call:

$this->_forward('action2');

If your second action is not in the same module/controller, you could subclass Zend_Controller_Action into your own "base" application controller that all your other action controllers inherit from (a good practice on ZF projects I find) and then create another one called _forwardFromUrl() or something like that, which breaks your URL apart and passes it to _forward() (or create an action controller helper if you just need this one extra thing).

This example is simplified and assumes your $url will always be in the format module/controller/action:

protected function _forwardFromUrl($url)
{
   $parts = array_reverse(explode("/",$url));
   $this->_forward($parts[0],$parts[1],$parts[2]);
}

Hope that helps!