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.
-
1preDispatch should work fine. How were you trying to access the models and what error(s) did you get?Tim Fountain– Tim Fountain2011-11-11 17:31:24 +00:00Commented Nov 11, 2011 at 17:31
-
Turns out my main problem is routing, the model works in my preDispatch. I will post the routing question separately. Thanks!jgnasser– jgnasser2011-11-20 18:34:17 +00:00Commented Nov 20, 2011 at 18:34
Add a comment
|
1 Answer
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
1 Comment
jgnasser
Thanks Pavel. As I wrote above, the main problem is routing. Once I figure that out I will try using a controller plugin as you suggest, looks good.