jgnasser
jgnasser

Reputation: 117

How to set layout based on a database value in Zend Framework

I need to switch the layout based on a user value that is stored in the database. I would like to set this using a plugin (tried PreDispatch hook). However, it looks like I can't access the models there as yet. At what point can I access db values and set the layout? I prefer to do this globally rather than set for each controller. Ideas appreciated.

Upvotes: 1

Views: 263

Answers (1)

Pavel Tytyuk
Pavel Tytyuk

Reputation: 119

For such purposes better to use controller plugin

class Core_Controller_Plugin_LayoutManager extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup (Zend_Controller_Request_Abstract $request)
    {
        // Get your layout name here

        $this->_layout = Zend_Layout::getMvcInstance()
            ->setLayoutPath(YOUR_PATH_HERE)
            ->setLayout(YOUR_LAYOT_NAME_HERE);
    }
}

Don't forget to add in in config:

resources.frontController.plugins.templatemanager = Core_Controller_Plugin_LayoutManager

Upvotes: 1

Related Questions