更改 Codeigniter 中的主题
我正在开发一个网站,其中添加了用户端主题的特征。为此,我创建了一个库。我在该库中编写了一个函数来加载 header.php 页面。
这是我的库代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_theme_lib {
public $theme_dir = "themes/winter";
public $header_page = "header.php";
public function get_header($header = ""){
$dir = $this->theme_dir;
$hdr = $this->header_page;
$this->CI =& get_instance();
$string = $this->CI->load->view($dir.'/'.$hdr, '', true);
return $string;
}
}
?>
在主题/冬天是我想要应用的主题文件夹。我创建了一个控制器来查看主题: 控制器代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class test extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library(array('theme_lib'));
}
public function index()
{
$this->load->view('themes/winter/index');
}
}
?>
在 theme/winter/index.php 中代码:
<?php
echo $res = $this->theme_lib->get_header();
?>
在 header.php 中:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
</head>
所以当我使用此代码时,我收到一条错误消息:
An Error Was Encountered
Unable to load the requested file: themes/winter/header.php
根文件夹中的 themes
。 那么如何从根文件夹目录而不是从视图目录加载视图页面呢?
Iam deveolping a site with adddition fetaure chening the themes of user side.For this i created a library .I wrote a function in that library to load header.php page.
This is my library code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_theme_lib {
public $theme_dir = "themes/winter";
public $header_page = "header.php";
public function get_header($header = ""){
$dir = $this->theme_dir;
$hdr = $this->header_page;
$this->CI =& get_instance();
$string = $this->CI->load->view($dir.'/'.$hdr, '', true);
return $string;
}
}
?>
In themes/winter is the my theme folder i want to apply.And i craeted a controller to view the theme:
Controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class test extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library(array('theme_lib'));
}
public function index()
{
$this->load->view('themes/winter/index');
}
}
?>
And in themes/winter/index.php code:
<?php
echo $res = $this->theme_lib->get_header();
?>
In header.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
</head>
So when i used this code i got an error message:
An Error Was Encountered
Unable to load the requested file: themes/winter/header.php
The themes
in the root folder.
So how can i load a view page from root folder directory not from view directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短回答:你不能。你不能,因为为了让事情更清晰,“系统”路径是固定的,这样你总是在模型文件夹中拥有模型,在视图文件夹中拥有视图等,因此你的结构将始终是规则的,实际的。想象一下,您将在几个月后维护此应用程序,此时您需要更改视图中的某些内容:它在哪里?我是否将其放在视图文件夹中或其他地方?主题?自定义视图?有什么看法?
保持事物严格分离有助于确保您的应用程序保持一致,并且无论谁在您之后查看代码,都可以轻松维护。即使那是你自己,因为很容易忘记你当时做了什么,即使是在你自己的代码中。
长答案(包括解决方案):好吧,实际上这可能比上面的短:),但是如果你转到 system/core/loader.php 文件中的第 130 行(至少在版本 2.1 中) .0;无论如何,它是 Loader 类构造函数),您会看到这一点:
这意味着您的视图前面始终是视图文件夹的硬编码路径。
解决方案?
[1]您可以删除“view/”部分,但这意味着您需要:
view()
方法并在其前面添加view/
,否则您的应用程序将无法挽回地崩溃;[2] 您可以做的另一件事是创建另一个路径,例如,
然后创建一个类似于加载器类的
view()
方法的方法(调用它的themes()
方法(如果您愿意),它将以完全相同的方式工作(包括缓冲),以便您能够调用控制器并加载主题,同时保留加载视图的能力。在这两个解决方案中,我肯定会选择这个解决方案。但您仍然需要要么不升级加载器类,要么在升级时添加更改。但至少这样您不会破坏任何核心功能。
对实用人士的额外答案:为什么不在
view
文件夹中创建一个themes
文件夹,并将所有主题放在那里呢?简单、干净、无麻烦:)Short answer: you can't. You can't because, to make things cleaner, the "system" paths are meant to be fixed, so that you always have models in the models folder, views in the views folder, etc. and therefore your structure will alwasy be regular and practical. Imagine you're going to mantain this app some months from now, when you need to change something in a view: where was it? Did I put it in thew views folder or somewhere else? themes? custom views? whatever views?
Keeping things strictly separated helps insure your application will be coherent, and easily mantained from whoever looks at the code after you. Even if that is you yourself, because it's easy to forget what you did when, even in your own code.
Long answer (solutions included): well, actually this might be shorted than the above :), but if you go to line 130 in the system/core/loader.php file (at least in version 2.1.0; anyway, it's the Loader class constructor), you'll see this:
which means your view will always be preceded by the hardcoded path to the view folder.
Solutions?
[1] You can remove the 'view/' part, but that would mean you need:
view()
method and prepend it withview/
, or your app will irremediably broke down;[2] Another thing you can do is create another path, like, for example,
and then create a method analogue to the
view()
method of the loader class (call itthemes()
method if you like), which will work the exact same way (buffering included) so that you'll have the ability to callin your controller and have your themes loaded, while preserving the ability to load view. I'll definitely go for this solution, among the two. But still you'll need either not to upgrade the loader class, or to add the changes when you do that. But you won't break any core functionality this way, at least.
Bonus answer for the practical people: why not just create a
themes
folder inside theview
folder, and put all your themes there? easy, clean, and hassle-free :)