Codeigniter - CI_Controller 与控制器 [英] Codeigniter - CI_Controller vs Controller

查看:23
本文介绍了Codeigniter - CI_Controller 与控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习一些关于 CodeIgniter 的教程,特别是关于扩展控制器的教程,我正在学习的教程说我应该在 MY_Controller 文件中放置以下内容:

 <?php if (!defined('BASEPATH')) exit('不允许直接脚本访问');类 MY_Controller 扩展控制器{函数 __construct(){parent::__construct();}}

但是,当我这样做时,我收到以下错误:

致命错误:在第 3 行的/home/chimeri1/public_html/application/libraries/MY_Controller.php 中找不到类Controller"

但是如果我把 CI_Cntroller 而不是 Controller 它工作.有什么区别?

还应该将关闭的 php 标记 ?> 添加到 MY_Controller 文件的底部,有些教程忽略了这一点?

提前致谢.

解决方案

在 2 之前的任何版本中,Controller 是基础控制器类(您可以使用 MY_Controller 扩展它).在版本 2 及更高版本中,您需要扩展 CI_Controller,因为这是基本控制器类的新名称.

对于遇到此问题的其他人,CI2 将几个类文件从 /libraries 移动到新的 /core 目录.如果您查看 system/core,您看到的每个类现在都使用了 CI_ 前缀 - 包括 Model(现在是 CI_Model)、Input、Loader、Lang 和好多其它的.如果您需要重载或扩展这些库,它们现在需要位于 application/core 目录中.您仍将使用 MY_ 前缀来扩展它们,或者您在 $config['subclass_prefix'] 中设置的任何内容.

随着版本 2 的发布,CI 分为两个分支:Core 和 Reactor.不要混淆 - 它们都是 CodeIgniter,但任何对Reactor"的引用都是当前社区驱动的版本,该版本作为网站上的主要下载提供,而 Core 是旧的 EllisLabs 版本,功能较少.来自 CI 网站:

<块引用>

简单地说,Reactor = CodeIgniter".

您可以在此处阅读有关分支更改的更多信息.

从 1.7.2 升级到 2.X 很容易.与往常一样,请务必阅读 更改日志升级说明 更新到新版本时.

就结束标签而言:它可选的,但您应该尽可能避免使用它,因为它会在脚本中引入空白(结束标签之后的任何内容),这可能会造成混乱标题并出现在您的输出中.如您所知,用户指南建议您不要使用它.>

I am trying to follow some tutorials regarding CodeIgniter, in particular ones about extending the controller, the tutorial I am following says that in the MY_Controller file I should put the following:

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

class MY_Controller extends Controller
{
    function __construct()
    {
        parent::__construct();
    }
}  

However when I do that I get the following error:

Fatal error: Class 'Controller' not found in /home/chimeri1/public_html/application/libraries/MY_Controller.php on line 3

If however I put CI_Cntroller instead of Controller it works. What is the differance?

Also should the closing php tag ?> be added to the bottom of the MY_Controller file, some tutorials miss this out?

Thanks in advance.

解决方案

In any version before 2, Controller is the base controller class (which you would extend with MY_Controller). In version 2 and up, you need to extend CI_Controller as this is the new name for the base controller class.

For anyone else coming across this, CI2 moved several class files from the /libraries to the new /core directory. If you take a look in system/core, every class you see there is now using the CI_ prefix - including Model (now CI_Model), Input, Loader, Lang, and many others. If you need to overload or extend these libraries, they now need to be in the application/core directory. You still will use the MY_ prefix to extend them, or whatever you have set in your $config['subclass_prefix'].

With the release of version 2, CI split into two branches: Core and Reactor. Don't be confused - they are both CodeIgniter, but any reference to "Reactor" is the current community driven version which is offered as the main download on the website, while Core is the older EllisLabs version with less features. From the CI website:

"Put simply, Reactor = CodeIgniter".

You can read more about the branch changes here.

Upgrading from 1.7.2 to 2.X is easy. As always, make sure to the read the change log and upgrade instructions when updating to a new version.

As far as the closing tag goes: it is optional, but you should avoid it when possible as it can introduce white space to the script (anything after the closing tag), which can mess up headers and appear in your output. As you know, the user guide recommends that you do not use it.

这篇关于Codeigniter - CI_Controller 与控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆