// application/core/MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 控制器扩展类
*
* 示例:
* ```
* $this->input->get('name');
* ```
*
* @property CI_Benchmark $benchmark
* @property CI_Config $config
* @property MY_DB_active_record $db
* @property CI_Exceptions $error
* @property CI_Hooks $hooks
* @property CI_Input $input
* @property CI_Lang $lang
* @property MY_Loader $load
* @property MY_Output $output
* @property CI_Router $router
* @property CI_Security $security
* @property CI_URI $uri
* @property CI_Utf8 $utf8
* @property CI_Smarty $smarty
*/
class MY_Controller extends CI_Controller
{
/**
* @var array 核心类
*/
protected $core_classes = [
'benchmark', 'common', 'config', 'controller', 'error' => 'exceptions', 'hooks',
'input', 'lang', 'load' => 'loader', 'output', 'router', 'security', 'uri', 'utf8',
];
/**
* @var array helper
*/
protected $helper_classes = [
'calendar', 'cart', 'calendar', 'cart', 'driver', 'email', 'encrypt', 'form_validation', 'ftp',
'image_lib', 'index.html', 'javascript', 'log', 'migration', 'pagination', 'parser', 'profiler',
'session', 'sha1', 'table', 'trackback', 'typography', 'unit_test', 'upload', 'user_agent',
'xmlrpc', 'xmlrpcs', 'zip',
];
protected $model = [
'user_model',
];
/**
* @inheritDoc
*/
public function __construct($autoload = true)
{
parent::__construct(false);
$this->load = load_class('Loader', 'core');
$this->load->initialize();
}
/**
* 根据属性名自动加载类
*
* 找不到属性时方法才会被调用
*
* @param string $name 属性名
* @return mixed
*/
function __get($name)
{
if ('db' == $name) {
$this->load->database();
} else {
if (isset($this->core_classes[$name])) {
$className = ucfirst($this->core_classes[$name]);
} else {
$className = ucfirst($name);
}
if (in_array($name, $this->helper_classes)) {
$this->load->library($className);
} elseif (in_array($name, $this->model)) {
$this->load->model($className, $name);
} else {
$this->$name =& load_class($className, isset($this->core_classes[$name]) || in_array($name, $this->core_classes) ? 'core' : 'libraries');
}
}
// $backtrace = debug_backtrace();
// if (!empty($backtrace[0])) {
// if (!property_exists($this, 'exceptions')) {
// $this->error = load_class('Exceptions', 'core');
// }
// return $this->error->show_php_error('Error', 'Undefined property: '.get_called_class().'::$'.$name, $backtrace[0]['file'], $backtrace[0]['line']);
// }
return $this->$name;
}
}