Zend Framework _forward for other actions within the same controller

How can I forward other actions within the same controller, avoiding repeating the entire sending process?

Example: If I point to User Controller, the default is indexAction () inside this function, I use _forwad ('list') ... but the whole sending process repeats .. and I don’t know what

What is the right way?

+3
source share
4 answers

( ) , (, Zend_Router). , ( " - " ) .

" script", ....

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

: , .

+6

  $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
+1

, , - .

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

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

You can also create a route. For example, in my /application/config/routes.ini 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 need only one action, and this is the index action, but the rss / feed request is also in progress.

public function indexAction()
{
    ...
}
0
source

Source: https://habr.com/ru/post/1752016/


All Articles