扩展 CodeIgniter 中的控制器类

发布于 2024-12-19 03:59:04 字数 400 浏览 5 评论 0原文

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

Fatal error: Class 'Profile' not found

CodeIgniter 尝试在 index.php 我正在运行。

我的错误在哪里?或者也许有另一种更好的方法来标记共同逻辑?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城仅有旧梦在 2024-12-26 03:59:05

我认为您已将 MY_Controller 放入 /application/core 中,并在配置中设置前缀。
不过,我会谨慎使用索引作为类名。作为 Codeigniter 中的函数/方法,它具有专用的行为。

如果您想扩展该控制器,则需要将这些类放在同一个文件中。

例如,在 /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 */

在 /application/controllers 中

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

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

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();
    }
...
}
过潦 2024-12-26 03:59:05

Codeigniter 3 可以实现这一点。只需包含父文件就足够了。

require_once(APPPATH."controllers/MyParentController.php");
class MyChildController extends MyParentController {
...

It is possible with Codeigniter 3. Just including the parent file is enough.

require_once(APPPATH."controllers/MyParentController.php");
class MyChildController extends MyParentController {
...
老子叫无熙 2024-12-26 03:59:05

不需要将父类复制/粘贴到所有控制器类中的解决方案:

  1. 将父类放入 core 文件夹中。

  2. 将 include 语句放置在包含父类的所有类的开头。

所以一个典型的控制器可能看起来像这样:

<?php

require_once APPPATH . 'core/Your_Base_Class.php';
// must use require_once instead of include or you will get an error when loading 404 pages

class NormalController extends Your_Base_Class
{
    public function __construct()
    {
        parent::__construct();
        
        // authentication/permissions code, or whatever you want to put here
    }

    // your methods go here
}

A solution that doesn't require copy/pasting the parent class into all your controller classes:

  1. Place your parent class in the core folder.

  2. Place an include statement at the beginning of all classes that include the parent class.

So a typical controller might look like this:

<?php

require_once APPPATH . 'core/Your_Base_Class.php';
// must use require_once instead of include or you will get an error when loading 404 pages

class NormalController extends Your_Base_Class
{
    public function __construct()
    {
        parent::__construct();
        
        // authentication/permissions code, or whatever you want to put here
    }

    // your methods go here
}
挽清梦 2024-12-26 03:59:05

您扩展的所有类都应位于 application/CORE 目录中,因此在您的情况下,My_Controller 和 Profile 都应位于该目录中。所有“端点”控制器都将位于应用程序/控制器文件夹中

更新

我纠正了。扩展类应该位于同一个文件中。 @Rooneyl 的答案显示了如何实施

All classes you are extending should live in application/CORE directory so in your case both My_Controller and Profile should live there. All "end point" controllers will live in application/controllers folder

UPDATE

I stand corrected. Extended classes should live in the same file. @Rooneyl's answer shows how to implement

枕梦 2024-12-26 03:59:05

经过与版本 3 和这个问题的一些斗争后,我认为这不是一个坏的解决方案...

require_once BASEPATH.'core/Controller.php';
require_once APPPATH.'core/MYCI_Controller.php';

在 system/core/CodeIgniter.php 中存在第一行的地方添加第二行

[如果还不算太晚,我建议强烈反对 php 和/或 CodeIgniter。]

After some struggle with version 3 and this issue I decided this was not a bad solution...

require_once BASEPATH.'core/Controller.php';
require_once APPPATH.'core/MYCI_Controller.php';

to add this second line where the first exists in the system/core/CodeIgniter.php

[If it's not too late, I recommend strongly against php and/or CodeIgniter.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文