2

我正在为我的项目使用 zend 框架,并且我需要将路径路由到我想要的位置。例如:我有一个路径 www.example.com/module/controller/action1,我想在内部路由到 www.example.com/module/controller/action2。

但主要问题是我不想使用必须指定模块、控制器和操作的函数 [$this->_forward('action2', 'controller', 'module');],只是我的东西像这样:$this->_forward('module/controller/action2');。

如果有人有解决方案,请建议我。它迫切需要我的项目。

谢谢,吉图

4

1 回答 1

0

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!

于 2011-01-11T18:31:07.377 回答