Zend Framework change view location in plugin
I cant find a solution to my problem anywhere, be it on SO or within the actual zend documentation.
Basically i have this setup:
I've created a plugin which using Zend_Http_UserAgent and WURFL to detect if the user is currently using a mobile. This is done in the predispatch. This works fine
I now want to change the view scripts directory if the user is using a mobile.
Ideally in a perfect world i'd like to try and load the mobile version of the view, if it exists, if not then load the default view. But if i can get 2 working at the least then i'll be happy.
I am more than stumped on how i do this. I've seen i could use:
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view; $view->开发者_JS百科setBasePath(APPLICATION_PATH . '/mobile_views/');
But that doesnt seem to do what i expect, plus this happens in postDispatch when i think this sort of thing should happen in preDispatch?
Here is my current plugin:
<?php
class SQ_Plugins_Mobile extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $flag) {
$bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap");
$useragent = $bootstrap->getResource("useragent");
$device = $useragent->getDevice();
Zend_Registry::set("useragent", $useragent);
Zend_Registry::set("device", $device);
echo $device->getType() . " is the type of device";
if($device->getType() != "mobile") {
/**
* Set the layout to be mobile, here we make sure we streamline what is loaded
* so as to not load things that arent needed.
*/
Zend_Layout::getMvcInstance()->setLayout("mobile");
/**
* Here i wish to change the view path to be APPLICATION_PATH . "/mobile_views/"
* @todo Change the view script path
*/
}
}
public function postDispatch(Zend_Controller_Request_Abstract $request) {
/**
* Maybe i have to change the view script path here?
* @todo change the viewscript path if we're using a mobile
*/
// Get the current view
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
$view->setBasePath(APPLICATION_PATH . '/mobile_views/');
echo "kl;kl;kjhkl;jhlkjhjklhkl;h k;hi";
}
}
In your action you could do this if it is a mobile version of the page.
$this->view->addBasePath('../application/views/mobile'); // Or whatever your path may be.
Just a thought.
OK, so i've fixed it, although i'm not sure it's the best most 'zend' solution, but i think it's quite elegant and works, which is the best one.
So now i check to see if the mobile version of the action view is available, if it is i change the extension to use a mobile suffix. This allows me to make sure i only load mobile views when i have them, meaning i reduce the potential for errors.
class SQ_Plugins_Mobile extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap");
$useragent = $bootstrap->getResource("useragent");
$device = $useragent->getDevice();
Zend_Registry::set("useragent", $useragent);
Zend_Registry::set("device", $device);
/**
* @todo change this to be Mobile
*/
if($device->getType() != "mobile") {
/**
* Set the layout to be mobile, here we make sure we streamline what is loaded
* so as to not load things that arent needed.
*/
Zend_Layout::getMvcInstance()->setLayout("mobile_layout");
/**
* Here we check to see if a mobile version of the template exists. if it does then we change the view suffix
* this allows us to load the mobile view if it exists and the defgault view if it doesnt.
*/
$base = APPLICATION_PATH . "/views/scripts/";
$mobile = $base . $request->getControllerName() . "/" . $request->getActionName() . ".mobile.phtml";
if(is_readable($mobile)) {
Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->setViewSuffix('mobile.phtml');
}
}
}
}
精彩评论