CI MY Loader错误

时间:2014-07-01 04:12:22

标签: codeigniter

我正在尝试在名为DIR_TEMPLATE的MY Loader父构造区域中使用自定义函数但是它没有将其拾起。

错误: Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\codeigniter\codeigniter-cms\catalog\core\MY_Loader.php on line 14

我希望能够在控制器中执行类似的操作。

if (file_exists(DIR_TEMPLATE . 'default' . '/template/common/header.tpl')) {
  return $this->load->view('default' . '/template/common/header.tpl', $data);
} else {
  return $this->load->view('default/template/common/header.tpl', $data);
}

MY_Loader.php

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

require APPPATH ."third_party/hmvc/loader.php";

class MY_Loader extends MX_Loader {

    public function __construct() {
        $this->_ci_view_paths = array(APPPATH . 'views/theme/' => TRUE);
        $this->_ci_ob_level  = ob_get_level();
        $this->_ci_library_paths = array(APPPATH, BASEPATH);
        $this->_ci_helper_paths = array(APPPATH, BASEPATH);
        $this->_ci_model_paths = array(APPPATH);

        DIR_TEMPLATE = $this->_ci_view_paths; // Error Here
        $this->_ci_view_paths = DIR_TEMPLATE; // Not working

        Severity: Notice

        Message: Use of undefined constant DIR_TEMPLATE - assumed 'DIR_TEMPLATE'

        Filename: core/MY_Loader.php

        Line Number: 14


        log_message('debug', "MY_Loader Class Initialized");
    }  

}

2 个答案:

答案 0 :(得分:1)

您需要定义DIR_TEMPLATE

 DIR_TEMPLATE = $this->_ci_view_paths; // Error Here

应该是

define( 'DIR_TEMPLATE',  $this->_ci_view_paths );

编辑: 因为它是array 您无法将其定义为PHP constant,因为它支持scalar and null only

你可以使用

define('DIR_TEMPLATE', serialize($this->_ci_view_paths));

当你需要它时,你可以unserialize()

答案 1 :(得分:0)

我现在就把它搞定了。我已经修改了My Loader的定义部分,但都很好。

加载控制器

if (file_exists(DIR_TEMPLATE .'default' . '/template/common/header.tpl')) {
  return $this->load->view('default' . '/template/common/header.tpl', $data);
} else {
  return $this->load->view('default/template/common/header.tpl', $data);
}

MY Loader.php

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

require APPPATH ."third_party/hmvc/loader.php";

class MY_Loader extends MX_Loader {

    public function __construct() {
        define('DIR_TEMPLATE', APPPATH . 'views/theme/');
      $this->_ci_view_paths = array(APPPATH . 'views/theme/' => TRUE);
      $this->_ci_ob_level  = ob_get_level();
        $this->_ci_library_paths = array(APPPATH, BASEPATH);
      $this->_ci_helper_paths = array(APPPATH, BASEPATH);
      log_message('debug', "MY_Loader Class Initialized");
   }  
}