Garethp Posted September 13, 2011 Share Posted September 13, 2011 Hey all, I'm fairly new to the more advanced functions of Zend such as plugins and contexts, and I'm a bit stuck, is there any chance you guys could help me out? Basically I'm creating a plugin that will look for json as a Param, and if it's in there, and the current view has another file with json as a suffix (such as view.json.phtml), to render that view instead. Here's the code <?php class Plugin_LayoutManager extends Zend_Controller_Plugin_Abstract { public function postDispatch() { $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $layout = Zend_Layout::getMvcInstance(); $view = $bootstrap->getResource('view'); $controller = $bootstrap->getResource('controller'); $controllerParam = $this->_request->getParam('controller'); $action = $this->_request->getParam('action'); $context = Zend_Controller_Action_HelperBroker::getStaticHelper('contextSwitch'); $context->initContext('json'); if(($json = $this->_request->getParam('json'))) { if($this->_scriptExists($controllerParam . "/" . $action. ".json")) { $context = Zend_Controller_Action_HelperBroker::getExistingHelper('contextSwitch'); $context->initContext('json'); } } } protected function _fileExists($file) { $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $layout = Zend_Layout::getMvcInstance(); $view = $bootstrap->getResource('view'); $paths = $view->getScriptPaths(); foreach ($paths as $path) { if (file_exists($path . $file . ".phtml")) { return true; } } return false; } protected function _scriptExists($file) { return $this->_fileExists($file); } } I've tried putting that in preDispatch, I've tried using $view->render(), I've tried getting the context a number of different ways. I'm at my wits end here. Any help would be appreciated. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/247020-zend-trouble-using-contextswitch-within-zend-plugin/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.