I want to use an interface inside the Codeigniter framework.
I'm using a fresh built Codeigniter 3.0 project, and by following a tutorial I created the following:
MY_Loader in application/core
<?php
class MY_Loader extends CI_Loader
{
/**
* List of paths to load interfaces from
*
* @var array
* @access protected
*/
protected $_ci_interface_paths = array();
function __construct()
{
parent::__construct();
//we do all the standard Loader construction, then also set up the acceptable places to look for interfaces
$this->_ci_interface_paths = array(APPPATH, BASEPATH);
}
public function initialize()
{
parent::initialize();
// After the parent is initialized, we load the autoload config
if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
}
else
{
include(APPPATH.'config/autoload.php');
}
// and if $autoload['interface'] is in the config, load each one
if (isset($autoload['interface']))
{
foreach($autoload['interface'] as $interface)
$this->iface($interface);
}
}
/**
* Load an interface from /application/interfaces
*
* @param $interface string The interface name
* @return CI_Loader for chaining */
public function iface($interface = '')
{
$class = str_replace('.php', '', trim($interface, '/'));
// Was the path included with the interface name?
// We look for a slash to determine this
$subdir = '';
if (($last_slash = strrpos($class, '/')) !== FALSE)
{
// Extract the path
$subdir = substr($class, 0, $last_slash + 1);
// Get the filename from the path
$class = substr($class, $last_slash + 1);
}
// Look for the interface path and include it
$is_duplicate = FALSE;
foreach ($this->_ci_interface_paths as $path)
{
$filepath = $path.'interfaces/'.$subdir.$class.'.php';
// Does the file exist? No? Try the remaining paths
if ( ! file_exists($filepath))
{
continue;
}
include_once($filepath);
$this->_ci_loaded_files[] = $filepath;
return $this;
}
}
}
/* End of file MY_Loader.php */
/* Location: ./application/core/MY_Loader.php */
Create new folder interfaces in application and under application/interfaces create file crudmodelinterface.php
<?php
interface crudmodelinterface {
public function create();
public function load();
public function update();
public function delete();
}
And in autoload.php in application/config I added this code:
$autoload['interface'] = array('crudmodelinterface');
In file controller Welcome.php I implemented crudmodelinterface
:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller implements crudmodelinterface{
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
public function create(){}
public function load(){}
public function update(){}
public function delete(){}
}
BUT when I access the controller it return this error:
Fatal error: Interface 'crudmodelinterface' not found in C:\xampp\htdocs\test_interface\application\controllers\Welcome.php on line 4
How to fix this error?