在CodeIgniter中扩展控制器类 [英] Extending The Controller Class in CodeIgniter
问题描述
我有类MY_Controller扩展CI_Controller
和大配置文件节的通用逻辑,所以I'va尝试创建 class Profile extends MY_Controller
具有公共逻辑的配置文件部分和所有类相关的这个部分应该扩展这个配置文件类,如我所知,但是当我试图创建 class Index profile Profile
我收到一个错误:
致命错误:找不到类'Profile'
/ pre>
CodeIgniter尝试在我正在运行的
index.php
中找到这个类。
我的错误在哪里?
解决方案我认为你把你的MY_Controller放在/ application / core,并在配置中设置前缀。
我会小心使用索引作为类名。作为Codeigniter中的一个函数/方法,它有一个专用的行为。
如果你想扩展该控制器,你需要把类放在同一个文件中。
例如In / application core
/ * php文件的开头* /
class MY_Controller extends CI_Controller {
public function __construct(){
parent :: __ construct();
}
...
}
class another_controller extends MY_Controller {
public function __construct(){
parent :: __ construct ;
}
...
}
/ * php文件结尾* /
在/ application / controllers
class foo extends MY_Controller {
public function __construct ){
parent :: __ construct();
}
...
}
或
class bar extends another_controller {
public function __construct(){
parent :: __ construct
}
...
}
I have
class MY_Controller extends CI_Controller
and common logic for big profile section, so I'va tried to createclass Profile extends MY_Controller
with common logic for profile section and all class related to this section should extends this Profile class as I understand right, but when I tried to createclass Index extends Profile
I recieve an error:Fatal error: Class 'Profile' not found
CodeIgniter tries to find this class in
index.php
which I am running.Where is my mistake? Or maybe there is anoter better way to mark out common logic?
解决方案I take it you have put your MY_Controller in /application/core, and set the prefix in the config. I would be careful about using index as a class name though. As a function/method in Codeigniter it has a dedicated behaviour.
If you then want to extend that controller you need to put the classes in the same file.
E.g. In /application core
/* start of php file */ class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); } ... } class another_controller extends MY_Controller { public function __construct() { parent::__construct(); } ... } /* end of php file */
In /application/controllers
class foo extends MY_Controller { public function __construct() { parent::__construct(); } ... }
or
class bar extends another_controller { public function __construct() { parent::__construct(); } ... }
这篇关于在CodeIgniter中扩展控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!