在 CodeIgniter 中扩展控制器类 [英] Extending The Controller Class in CodeIgniter

查看:28
本文介绍了在 CodeIgniter 中扩展控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 class MY_Controller extends CI_Controller 和大配置文件部分的通用逻辑,所以我试图创建 class Profile extends MY_Controller 与配置文件部分和所有的通用逻辑与本节相关的类应该按照我的理解扩展这个 Profile 类,但是当我尝试创建 class Index extends Profile 时,我收到一个错误:

I have class MY_Controller extends CI_Controller and common logic for big profile section, so I'va tried to create class 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 create class Index extends Profile I recieve an error:

Fatal error: Class 'Profile' not found

CodeIgniter 尝试在我正在运行的 index.php 中找到这个类.

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?

推荐答案

我认为您已将 MY_Controller 放在/application/core 中,并在配置中设置前缀.不过,我会小心使用索引作为类名.作为 Codeigniter 中的一个函数/方法,它有一个专门的行为.

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.

例如在/应用程序核心

/* 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 */

在/application/controllers

In /application/controllers

class foo extends MY_Controller {
    public function __construct() {
       parent::__construct();
    }
...
}

class bar extends another_controller {
    public function __construct() {
       parent::__construct();
    }
...
}

这篇关于在 CodeIgniter 中扩展控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆