【问题标题】:Extending The Controller Class in CodeIgniter在 CodeIgniter 中扩展控制器类
【发布时间】:2012-01-10 15:36:31
【问题描述】:

我有class MY_Controller extends CI_Controller 和大型配置文件部分的通用逻辑,所以我尝试使用配置文件部分的通用逻辑创建class Profile extends MY_Controller,并且所有与此部分相关的类都应该扩展此配置文件类,因为我理解正确,但是当我尝试创建 class Index extends Profile 时,我收到一个错误:

Fatal error: Class 'Profile' not found

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

我的错误在哪里?或者也许有其他更好的方法来标记常见的逻辑?

【问题讨论】:

标签: php codeigniter codeigniter-2


【解决方案1】:

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

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

例如在 /application 核心中

/* 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();
    }
...
}

【讨论】:

  • 可能值得一提的是MY_Controller 中的方法应该可能以_ 为前缀,这样 CI 就不会路由它们(因为本质上所有方法都是你想要的与所有控制器共享必须是公开的)。
  • 当然可以保护共享方法而不是公共方法。所以它们不会被路由,但可以被扩展控制器访问。
【解决方案2】:

我在 Google 上找到了这个页面,因为我遇到了同样的问题。我不喜欢这里列出的答案,所以我创建了自己的解决方案。

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
}

我喜欢这个解决方案的原因是,创建父类的全部目的是减少代码重复。所以我不喜欢建议将父类复制/粘贴到所有控制器类中的其他答案。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      更新

      我的立场是正确的。扩展类应该存在于同一个文件中。 @Rooneyl 的回答展示了如何实现

      【讨论】:

      • 我认为没有必要所有控制器都在 CORE 下,而只有基本控制器。
      • @luso,我尝试将 my_controllers 放入 /controllers/core 并将 Profile & OtherClass(操作称为索引)放入 /controllers。其他类扩展 Profile 扩展 My_controller。这会引发错误。将 Profile 移至 /core 即可。你能把 Profile 和 OtherClass 放在 /controllers 中并让它工作吗?
      • @Alexey Gerasimov 我已将 Profile 类移至 application/core 文件夹,但没有任何改变。我确实收到了同样的错误。什么名字有文件或其他东西有关系吗?我错过了什么?
      • @Rooneyl 我明白了。但是创建__autoload 函数是一种好习惯,还是更好地将所有常用类放在MY_Controller 中?
      【解决方案5】:

      在与版本 3 和这个问题进行了一番斗争之后,我认为这不是一个糟糕的解决方案......

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

      在 system/core/CodeIgniter.php 中第一行的位置添加第二行

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

      【讨论】:

      • 在投反对票前说明原因。
      • 他们很可能喜欢 php,如果我冒犯了任何人,我深表歉意。
      • 他们不喜欢 PHP... 并非所有框架都提供文件处理并且总是需要改进。
      猜你喜欢
      • 2015-01-22
      • 2017-09-13
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多