Php codeigniter,类未从库文件夹扩展
这就是我所做的Php codeigniter,类未从库文件夹扩展,php,codeigniter,Php,Codeigniter,这就是我所做的 applications/config/config.php文件中的我的设置 $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO'; $config['url_suffix'] = ''; $config['subclass_prefix'] = 'MY_'; 在application/core MY_Controller.php文件包括: class MY_Controller extends CI_Cont
applications/config/config.php
文件中的我的设置
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
$config['subclass_prefix'] = 'MY_';
在application/core
MY_Controller.php文件包括:
class MY_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
}
}
在应用程序/库中创建文件Frontend_Controller.php
Frontend_Controller.php文件包括
class Frontend_Controller extends MY_Controller
{
public function __construct(){
parent::__construct();
}
}
最后,我用前端控制器扩展了这里的主控制器类
我的主控制器位于application/controllers/main.php
class Main extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('PrizeBondSearch_Model');
}
public function index()
{
$PrizeBonds = $this->PrizeBondSearch_Model->ShowAllPBS();
$this->load->view('home', $PrizeBonds);
}
}
问题:
所以问题来了,当我用MY_controller
扩展主控制器类时,它工作得非常好
但是当我尝试用Frontend\u controller
类扩展主控制器时,它给了我以下问题
致命错误:在中找不到类“前端_控制器”
C:\xampp\htdocs\projects\PrizeBondSearch\application\controllers\main.php
在线3
有什么解决办法吗?不用担心,终于找到了解决办法
需要加载库类名
因此,在config.php文件中添加了以下行
function __autoload($classname){
if(strpos($classname, 'CI_')!==0){
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file)&& is_file($file)){
@include_once($file);
}
}
}
它现在工作得非常好。在MY_Controller.php的开头包含Frontend_Controller.php,或者使用spl_autoloader来防止此错误。php中不推荐接受答案的某些内容
在config.php文件中使用此函数
spl_autoload_register(function ($classname) {
if (strpos($classname, 'CI_') !== 0)
{
$file = APPPATH . 'libraries/' . $classname . '.php';
if (file_exists($file) && is_file($file))
{
@include_once($file);
}
}
});
为什么要将控制器放在库文件夹中?这正是tutsplus视频教程中的问题和答案,对吗?您应该将控制器存储在应用程序/controllers
中。库目录不适用于控制器。你的解决方案是不好的做法。@crypticツ 不,我知道我想要的是基础控制器。不仅仅是控制器。