-
Notifications
You must be signed in to change notification settings - Fork 26
Java Like Import
This is a primitive java like import for CI.
You must enable hooks first at yout config.php [code] $config['enable_hooks'] = TRUE; [/code]
Add a hooks configuration at hooks.php [code] $hook['pre_system'] = array( 'class' => '', 'function' => 'fake', 'filename' => 'ClassLoader.php', 'filepath' => 'hooks' ); [/code]
Create ClassLoader.php under hooks directory [code] <?php function fake(){ // do nothing }
function import($class, $package = 'package')
{
$class = $package . "/" . $class;
$class = str_replace(".", "/", $class);
$path = APPPATH . $class . EXT;
if(file_exists($path)){
require_once($path);
return true;
}else{
return false;
}
} ?> [/code]
Create a folder named 'package' under application directory. This directory is the home for your own class.
Example: We create a class BaseController on package/com/company/project/controller/BaseController.php
[code] <?php // extends CI Controller class BaseController extends Controller {
function BaseController(){
parent::Controller();
}
}
?> [/code]
At your regular controller you can import base controller like this: [code] <?php
import('com.company.project.controller.BaseController'); // you can import other class here
class Homepage extends BaseController{
function Homepage(){
parent::BaseController();
}
function index(){
echo "test";
}
} ?> [/code]