2010-06-27 94 views
3

如何转发到同一控制器内的其他动作,避免重复所有的调度进程?Zend Framework _forward在同一控制器内的其他动作

例子: 如果我指向用户控制器的默认操作为的indexAction()这个函式里面我使用_forwad(名单')......但所有调度proccess重复..我不认为

什么是正确的方式?

回答

6

通常,您将安装路径以将您的用户重定向到适当的(默认)操作,而不是索引操作(请阅读如何使用Zend_Router从给定路径重定向)。但是如果你真的想要直接从控制器中获得(但是这被称为“编写黑客代码来实现某些脏东西”),你可以手动执行所有操作。

更改“查看脚本”被渲染,然后打电话给你的操作方法....

// inside your controller... 
public function indexAction() { 
    $this->_helper->viewRenderer('foo'); // the name of the action to render instead 
    $this->fooAction(); // call foo action now 
} 

如果你倾向于使用这种“把戏”的时候,也许你可以写一个基本的控制器,你延长你的应用程序,它可以简单地有一个方法,如:

abstract class My_Controller_Action extends Zend_Controller_Action { 
    protected function _doAction($action) { 
     $method = $action . 'Action'; 
     $this->_helper->viewRenderer($action); 
     return $this->$method(); // yes, this is valid PHP 
    } 
} 

然后从你的行动调用的方法...

class Default_Controller extends My_Controller_Action 
    public function indexAction() { 
     if ($someCondition) { 
     return $this->_doAction('foo'); 
     } 

     // execute normal code here for index action 
    } 
    public function fooAction() { 
     // foo action goes here (you may even call _doAction() again...) 
    } 
} 

备注:这不是官方的做法,但它的一个解决方案。

0

如果您不想重新发送,则没有理由不能简单地调用该操作 - 它只是一个函数。

class Default_Controller extends My_Controller_Action 
{ 
    public function indexAction() 
    { 
     return $this->realAction(); 
    } 

    public function realAction() 
    { 
     // ... 
    } 
} 
0

您也可以创建一个路线。例如,我在我的/application/config/routes.ini一节:

; rss 
routes.rss.route    = rss 
routes.rss.defaults.controller = rss 
routes.rss.defaults.action  = index 

routes.rssfeed.route    = rss/feed 
routes.rssfeed.defaults.controller = rss 
routes.rssfeed.defaults.action  = index 

现在你只需要一个动作,那就是指数的行动,但requess RSS /饲料也去那里。

public function indexAction() 
{ 
    ... 
} 
+1

不,谢谢,我想从控制器,使用前路由重定向... – ovitinho 2013-08-02 18:09:22

1

我们还可以用这个帮手重定向

$this->_helper->redirector->gotoSimple($action, $controller, $module, $params); 

    $this->_helper->redirector->gotoSimple('edit'); // Example 1 

    $this->_helper->redirector->gotoSimple('edit', null, null, ['id'=>1]); // Example 2 With Params 
相关问题