为移动设备开发

时间:2011-11-28 23:55:18

标签: php opencart

这是当今的一个热门话题,我需要为我的网站构建一个基于jQuery Mobile的模板。构建模板不是问题,而是在有人通过移动设备进行导航时显示。我知道我需要更改OC核心中的一些代码才能做到这一点,但需要一些建议或帮助。首先,加载模板的地方是/system/engine/controller.php。这是功能:

    protected function render() {
      foreach ($this->children as $child) {
         $this -> data[basename($child)] = $this -> getChild($child);
      }

      if (file_exists(DIR_TEMPLATE . $this -> template)) {
         extract($this -> data);
         ob_start();
         require (DIR_TEMPLATE . $this -> template);
         $this -> output = ob_get_contents();
         ob_end_clean();
         return $this -> output;
      } else {
         exit('Error: Could not load template ' . DIR_TEMPLATE . $this -> template . '!');
      }
   }

好的,我管理如何处理以检查用户代理是否是移动设备,这是结果:

 protected function render() {
      foreach ($this->children as $child) {
         $this -> data[basename($child)] = $this -> getChild($child);
      }

      //--------- ADDED -------------------------------------------------
      if ($this -> config -> get('mobile_status') == 1) {
         if (($this -> isMobile() && $this -> config -> get('autodetect') == 'true') || $this -> session -> data['ismobile'] == 1) {
            $mobile_template = $this -> config -> get('mobile_template_name');
            if ($mobile_template != '') {
               if (!function_exists('is_dir') || (function_exists('is_dir') && is_dir(DIR_TEMPLATE . $mobile_template))) {
                  $this -> template = $mobile_template . "/";
               }
            }
         }
      }
      //--------- ADDED -------------------------------------------------

      if (file_exists(DIR_TEMPLATE . $this -> template)) {
         extract($this -> data);
         ob_start();
         require (DIR_TEMPLATE . $this -> template);
         $this -> output = ob_get_contents();
         ob_end_clean();
         return $this -> output;
      } else {
         exit('Error: Could not load template ' . DIR_TEMPLATE . $this -> template . '!');
      }
   }

现在,当我尝试使用移动用户代理访问时,我收到此错误:

  

D:\ Webserver \ htdocs \ portal / catalog / view / theme / libcommerce_mobile / Warning:require(D:\ Webserver \ htdocs \ portal \ catalog \ view \ theme \ libcommerce_mobile)[function.require]:无法打开stream:第77行的D:\ Webserver \ htdocs \ portal \ system \ engine \ controller.php中的权限被拒绝   致命错误:require()[function.require]:无法打开所需的'D:\ Webserver \ htdocs \ portal / catalog / view / theme / libcommerce_mobile /'(include_path ='。; D:\ Webserver \ php \ PEAR')在第77行的D:\ Webserver \ htdocs \ portal \ system \ engine \ controller.php

但令人惊讶的是目录存在且可读,对此有何帮助?我做错了什么? 再欢呼一次

PS:来自此主题http://forum.opencart.com/viewtopic.php?f=20&t=47124

1 个答案:

答案 0 :(得分:1)

问题是$this->template包含文件的整个路径,而不仅仅是模板目录。你应该做这样的事情

    if($this->config->get('mobile_status') == 1) {
        if(($this->isMobile() && $this->config->get('autodetect') == 'true') || $this->session->data['ismobile'] == 1) {
            $template = preg_replace('~^[^/]+~', $this->config->get('mobile_template_name'), $this->template);
            if(file_exists(DIR_TEMPLATE . $template) && !is_dir(DIR_TEMPLATE . $template)) {
                $this->template = $template;
            }
        }
    }