Zend Framework – Disable Layout for exporting CSV file

I am using a plugin to pull up my layouts specific to the module that the user is in. I needed to be able to disable the layout, but still render the view because the view was where I compile the entire report for our company.

Here is my Layout Plugin:

class Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
        $module = $request->getModuleName();

        $front_controller = Zend_Controller_Front::getInstance();
        $error_handler = $front_controller->getPlugin('Zend_Controller_Plugin_ErrorHandler');
	$error_handler->setErrorHandlerModule($module);
        
    }
    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {

        $front_controller = Zend_Controller_Front::getInstance();
        $module_directory = $front_controller->getModuleDirectory();
        
        $layout = Zend_Layout::getMvcInstance();
        
        $layout->setLayout('layout');
        $layout->setLayoutPath($module_directory . DIRECTORY_SEPARATOR . 'views/layouts');
        
        if(isset($_REQUEST['format']) && $_REQUEST['format'] != 'html') {
        
            $layout->disableLayout();
            
        }

    }
		
}

You can see that inside my postDispatch method I have included that if there is a request variable set to ‘format’ (that is Post/Get/Cookie) and it is not equal to ‘html’, then it will render the page without a layout. By doing this it allows me to easily add other reports that are not created for use with an MVC structure, quickly and effortlessly – in many cases what you may do instead is set the layout disabling inside a particular action and also disable the view, but because the reports contain the data that creates the CSV I need the view to Render. Any questions or comments – feel free to leave a note!

Friday, October 23rd, 2009 Zend Framework

Leave a Reply

You must be logged in to post a comment.

[ad code=1 align=center]