我无法在任何地方找到问题的解决方案,无论是在SO上还是在实际的zend文档中。
基本上我有这个设置:
我创建了一个插件,它使用Zend_Http_UserAgent和WURFL来检测用户当前是否正在使用移动设备。这是在predispatch中完成的。这很好用
如果用户使用手机,我现在想要更改视图脚本目录。
理想情况下,在一个完美的世界中,我想尝试加载视图的移动版本(如果存在),如果不存在则加载默认视图。但是,如果我能够至少工作2,那么我会很开心。
我不知道如何做到这一点。我见过我可以用:
$ view = Zend_Controller_Action_HelperBroker :: getStaticHelper('viewRenderer') - > view; $ view-> setBasePath(APPLICATION_PATH。'/ mobile_views /');
但这似乎没有达到我的预期,加上这种情况发生在postDispatch中,我认为这种事情应该在preDispatch中发生?
这是我目前的插件:
<?php
类SQ_Plugins_Mobile扩展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";
}
}
答案 0 :(得分:1)
在您的操作中,如果是页面的移动版本,则可以执行此操作。
$this->view->addBasePath('../application/views/mobile'); // Or whatever your path may be.
只是一个想法。
答案 1 :(得分:0)
好的,所以我已经修好了,虽然我不确定它是最好的'zend'解决方案,但我觉得它非常优雅且有效,这是最好的。
所以现在我检查动作视图的移动版本是否可用,如果是我更改扩展名以使用移动后缀。这使我能够确保我只有在拥有移动视图时才加载移动视图,这意味着我减少了出错的可能性。
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');
}
}
}
}