【问题标题】:Zend Framework change view location in pluginZend Framework 在插件中更改视图位置
【发布时间】:2011-03-18 15:48:12
【问题描述】:

我无法在任何地方找到解决问题的方法,无论是在 SO 上还是在实际的 zend 文档中。

基本上我有这个设置:

  1. 我创建了一个插件,它使用 Zend_Http_UserAgent 和 WURFL 来检测用户当前是否正在使用手机。这是在预调度中完成的。这很好用

  2. 如果用户使用移动设备,我现在想更改视图脚本目录。

  3. 理想情况下,我想尝试加载视图的移动版本,如果存在,则加载默认视图。但是,如果我至少可以让 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


【解决方案1】:

如果是移动版页面,您可以执行此操作。

$this->view->addBasePath('../application/views/mobile'); // Or whatever your path may be.

只是一个想法。

【讨论】:

    【解决方案2】:

    好的,所以我已经修复了它,虽然我不确定它是否是最好的最“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');      
            } 
    
        }
    }
    }
    

    【讨论】:

    • 很明显,在上面的示例中,我每次都强制视图成为移动视图。 $device->getType() == "mobile" 应该是这样的逻辑
    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多