Codeigniter - CI_Controller和Controller [英] Codeigniter - CI_Controller vs Controller

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

问题描述

我想跟着一些关于CodeIgniter的教程,特别是关于扩展控制器的教程,下面的教程说,在MY_Controller文件中我应该添加以下内容:

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

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

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



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

但是如果我把 CI_Cntroller 而不是 Controller 。有什么区别?



也应该将关闭php标记?> 添加到MY_Controller文件的底部,



提前感谢。

解决方案

before 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很容易。一如既往,请务必阅读更改日志升级说明更新到新版本。



对于结束标记: / em>可选,但你应该避免它,当可能的话,因为它可以引入空白的脚本(任何后面的标签),这可能会弄乱标题,并出现在您的输出。如您所知,用户指南建议您不要使用它


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和Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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