Zend Framework _forward 到同一控制器内的其他操作

发布于 2024-09-07 23:44:05 字数 143 浏览 2 评论 0原文

我如何转发到同一控制器内的其他操作,以避免重复所有调度过程?

例子: 如果我指向用户控制器,默认操作是该函数内的indexAction(),我使用_forwad('list') ...但是所有调度过程都会重复..我不知道

什么是正确的方法?

How can i forward to other action inside the same controller avoiding repeat all dispatch proccess ?

Example:
If i point to User Controller the default action is indexAction() inside this funciton i use _forwad('list') ... but all dispatch proccess are repeated.. and i dont that

Whats is the right way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄人书 2024-09-14 23:44:06

通常,您将安装路由来将用户重定向到正确的(默认)操作,而不是索引操作(阅读如何使用 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
}

如果您倾向于经常使用此“技巧”,也许您可​​以编写一个在应用程序中扩展的基本控制器,它可以简单地有一个方法like :

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

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

Usually, you will install routes to redirect your users to the proper (default) action, instead of the index action (read how to redirect from a given route using Zend_Router). But you can do everything manually if you really want to (however this is called "writing hacker code to achieve something dirty") directly from the controller.

Change your "view script" to be rendered, then call your action method....

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

If you tend on using this "trick" often, perhaps you may write a base controller that you extend in your application, which can simply have a method like :

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
   }
}

Then call the method from your action...

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

NOTE : this is not the official way to do it, but it is a solution.

违心° 2024-09-14 23:44:06

我们还可以使用这个 Helper 来重定向

  $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

We Can Also use this Helper To redirect

  $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
黄昏下泛黄的笔记 2024-09-14 23:44:06

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

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

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

If you don't want to re-dispatch there is no reason you can't simply call the action - it's just a function.

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

    public function realAction()
    {
        // ...
    }
}
只是偏爱你 2024-09-14 23:44:06

您还可以创建一条路线。例如,我在 /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

现在您只需要一个操作,即索引操作,但请求 rss/feed 也在那里。

public function indexAction()
{
    ...
}

You could also create a route. For example I have in my /application/config/routes.ini a section:

; 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

Now you only need one action and that is index action but the requess rss/feed also goes there.

public function indexAction()
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文