ZendFramework Send variables from Controller to View (Best pactice)

I have been working in the Zend Framework for a while, and am currently reorganizing some parts of my code. One of the important points that I would like to eliminate is my controller class abstract, which initiates many variables that should be present throughout my controller, such as $success, $warningand $error. This part can be done in pluggins controllers, but what would be the best way to send these variables to the appropriate form. I am currently using my own method in a class abstractthat I call from all my controllers.

protected function sendViewData(){
    $this->view->success  = $this->success;
    $this->view->warning  = $this->warning;
    $this->view->error    = $this->error;
}

which is then called in all the actions of all my controllers through t

parent::sendViewData();

I tried to automate this process through the plugin controller or something more suitable for this

+1
source share
2 answers

You can set the postDisplatch method in your abstract controller to initialize view data (see the section "Pre-and post-dispatch hooks") ..

Thus, in each action, you can initialize the variables $this->success, $this->warnningor $this->error, and this will be passed to the view after the action is completed.

+5
source

The best pactice is to define a base controller and allow other controllers to extend it, instead of directly calling a methodZend_Controller_Action

// Your base controller file ApplicationController.php
class ApplicationController extends Zend_Controller_Action {
       // method & variable here are available in all controllers
        public function preDispatch() {
            $this->view->success  = $this->success;
            $this->view->warning  = $this->warning;
            $this->view->error    = $this->error;
        }
}

Your other regular controllers will be like

// IndexController.php
class IndexController extends ApplicationController {

}

(, ) views/layout, In ApplicationController.php .

+2

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


All Articles