简体   繁体   中英

How to disable a view script in a controller - Zend Framework

I am playing with zend framework's MVC. One thing I found out is that Zend Framework tries to include a view script whenever a controller is called. I can disable it in bootstrap with the following code.

$frontController->setParam('noViewRenderer',true);

However, I have to initialize Zend_View class in a controller method then render a script file myself.

How can I stop including a view script in a controller method so I can disable it if only I want to?

you can disable the view renderer controller helper, with this code in your controller:

public function myAction()
{
    $this->_helper->viewRenderer->setNoRender(true);
    // from now on, ZF won't search for a matching view script file.
}

The best example would be to use both commands above:

public function myAction() {
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}

First one disables layout,in general is enabled

application.ini

default

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

$this->_helper->layout->disableLayout();

and second disables view script (.phtml) so exception is not thrown if view script file is not found

$this->_helper->viewRenderer->setNoRender(true);

There are also view helpers that include bouth listened abowe and are not necessary, for example you want to return JSON from array data and not render view element.

public function myAction() {
    $this->_helper->json(array());
}

will not render layout nor view script.

Easy, just disable it from within your action.

$this->_helper->layout->disableLayout();

If you aren't talking about layouts, then just add an exit() to your action. Just understand what sort of impact that will have on your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM