Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - CI_Controller vs Controller

Tags:

codeigniter

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.

like image 562
James Avatar asked Apr 10 '11 18:04

James


Video Answer


1 Answers

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.

like image 106
Wesley Murch Avatar answered Sep 20 '22 17:09

Wesley Murch