【发布时间】:2011-03-18 15:48:12
【问题描述】:
我无法在任何地方找到解决问题的方法,无论是在 SO 上还是在实际的 zend 文档中。
基本上我有这个设置:
我创建了一个插件,它使用 Zend_Http_UserAgent 和 WURFL 来检测用户当前是否正在使用手机。这是在预调度中完成的。这很好用
如果用户使用移动设备,我现在想更改视图脚本目录。
理想情况下,我想尝试加载视图的移动版本,如果存在,则加载默认视图。但是,如果我至少可以让 2 个工作,那么我会很高兴。
我不知道我是如何做到这一点的。我已经看到我可以使用:
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view; $view->setBasePath(APPLICATION_PATH . '/mobile_views/');
但这似乎并没有达到我的预期,而且当我认为这种事情应该发生在 preDispatch 中时,这会发生在 postDispatch 中?
这是我当前的插件:
<?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";
}
}
【问题讨论】:
-
你说得对,视图变化应该在preDispatch()中设置。
-
如果你还是要将用户代理和设备保存在注册表中,我可能会在 Bootstrap 中进行操作。
-
如果您提供的内容依赖于用户代理,那么您将拥有相同的 url,根据他们的代理向不同的客户提供不同的内容。我可能会为移动版本创建一个子域;将子域指向与 Web 空间相同的应用程序空间;在插件中进行代理检测,当用户代理符合条件时重定向到移动子域;链接一个主机名路由,为子域的任何请求添加一个标志;然后在另一个插件中检查该标志以设置布局/视图/后缀。只是大声思考。
标签: php zend-framework plugins