开发者

Codeigniter - CI_Controller vs Controller

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 thi开发者_如何学Cs 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.


In CodeIgniter 2.x, the Controller class has been renamed to CI_Controller. The tutorial you're using is most likely using CodeIgniter 1.7 or earlier, when Controller was the correct class to extend.

Regarding the closing ?> tag, the CodeIgniter style guide recommends omitting it, since accidental white space after that tag can be problematic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜