According to this Extending core CI Classes , I can create a class in my application/core/MY_Protectedcontroller.php as:
//e.g. I want to extend core CI_Controller class.So
class MY_Protectedcontroller extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('permission');
$this->load->library('authentication');
}
}
Well as you might discern my objective here..actually I want the auth and perm libraries to be available to all of my controllers thus I can authenticate very easily and programmatically. But when I tried to do:
class User extends MY_Protectedcontroller{
function __construct(){
parent::__construct();
.....// relevant codes
.....//relevant codes
}
}
It says the "Not Found" thing. Fatal error: Class 'MY_Protectedcontroller' not found in C:\xampp\htdocs\damcombd\application\controllers\admin\user.php on line 3
You have to name your class file MY_Controller.php
, NOT MY_Protectedcontroller.php
.
As you can see in the user guide, in the System Class list, MY_Protectedcontroller
is not there. You have to extend an existing class, and you have to name it appropriately for CodeIgniter to find it. In this case, we're extending Controller
, so you have to name the file MY_Controller.php
so that CodeIgniter recognizes and finds it (you can change MY_
in the config as you've found). If we were trying to extend the Input class, we would name the file MY_Input.php
and so on.
Download Codeigniter 2.1.4 and put it somehwere.
Create MY_Controller.php
and put it in application/core/MY_Controller.php
:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Protectedcontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Create a controller test.php
and put it in controllers/test.php
:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends MY_Protectedcontroller
{
function index()
{
echo "test";
}
}
and visit http://localhost/ci-2.1.4/index.php/test
Well just adding require_once made it work..It's something I dont like though. Coz, the Ellislab doc never says that I need to do this thing.. any way this worked for me:
require_once APPPATH.'core/MY_Protectedcontroller.php';
class User extends MY_Protectedcontroller{
function __construct(){
parent::__construct();
.....// relevant codes
.....//relevant codes
}
}
You seem to be targeting some very advanced functionality.
Would you ever consider loading the permission and authentication libraries in the same way that CodeIgniter automatically loads the /system/core/Input.php
file? This is why you can easily call $this->input->post();
If you are willing to mess with the system directory files then I think it should be as simple as finding line #208 in /system/core/CodeIgniter.php
and make it look like this
/* Line 208 starts here
* ------------------------------------------------------
* Load the Input class and sanitize globals
* ------------------------------------------------------
*/
$IN =& load_class('Input', 'core');
/*
* ------------------------------------------------------
* Load the Permission class
* ------------------------------------------------------
*/
$PERM =& load_class('Permission', 'libraries/custom');
/*
* ------------------------------------------------------
* Load the Authentication class
* ------------------------------------------------------
*/
$AUTH =& load_class('Authentication', 'libraries/custom');
And make sure you place your libraries in the proper directories like so:
/system/libraries/custom/Permission.php
/system/libraries/custom/Authentication.php
This should now give you global access to $this->authentication->someFunc();
and $this->permission->someFunc();
If you want to find the load_class()
function then go to line #133 in /system/core/Common.php
and look for function &load_class()