如何从此 Codeigniter 3 应用程序中的默认视图文件夹外部加载视图?
How do I load a view from outside the default views folder in this Codeigniter 3 application?
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 CMS 4. 我已决定为其添加主题。该应用程序不是 HMVC,只有 MVC。
themes 目录在 application 之外,如下图所示:
在 themes 中,我有主题目录(当然),其中包含“主视图”,layout.php
:
在 application/core
中,我添加了一个 Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH."../system/core/Loader.php";
class MY_Loader extends CI_Loader {
function ext_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}?>
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$this->load->ext_view('third_party', 'themes/caminar/layout', $data);
}
我收到此错误消息:
Call to undefined method CI_Loader::ext_view();
我做错了什么?
我是这样解决问题的:
在 application/core
中,我添加了一个 MY_Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
function theme_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(FCPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->load->theme_view('/themes/caminar/', 'layout', $data);
}
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 CMS 4. 我已决定为其添加主题。该应用程序不是 HMVC,只有 MVC。
themes 目录在 application 之外,如下图所示:
在 themes 中,我有主题目录(当然),其中包含“主视图”,layout.php
:
在 application/core
中,我添加了一个 Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH."../system/core/Loader.php";
class MY_Loader extends CI_Loader {
function ext_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}?>
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$this->load->ext_view('third_party', 'themes/caminar/layout', $data);
}
我收到此错误消息:
Call to undefined method CI_Loader::ext_view();
我做错了什么?
我是这样解决问题的:
在 application/core
中,我添加了一个 MY_Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
function theme_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(FCPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->load->theme_view('/themes/caminar/', 'layout', $data);
}