Jump to content

[Zend] Trouble using ContextSwitch within Zend Plugin


Garethp

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.