Jump to content

[Zend] Trouble using ContextSwitch within Zend Plugin


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.