php 在 CodeIgniter 中扩展控制器类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8342232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:29:26  来源:igfitidea点击:

Extending The Controller Class in CodeIgniter

phpcodeignitercodeigniter-2

提问by Yekver

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

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

Fatal error: Class 'Profile' not found

CodeIgniter tries to find this class in index.phpwhich I am running.

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

Where is my mistake? Or maybe there is anoter better way to mark out common logic?

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

回答by Rooneyl

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.

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

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

回答by AdmiralThrawn

I found this page on Google because I had the same problem. I didn't like the answers listed here so I created my own solution.

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

1) Place your parent class in the corefolder.

1)将您的父类放在core文件夹中。

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

2) 在包含父类的所有类的开头放置一个 include 语句。

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
}

The reason I like this solution is, the whole point of creating a parent class is to cut down on code repetition. So I don't like the other answer that suggested copy/pasting the parent class into all of your controller classes.

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

回答by Cem Y?ld?z

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

Codeigniter 3 是可能的。只包含父文件就足够了。

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

回答by Alexey Gerasimov

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

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

UPDATE

更新

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

我站着纠正。扩展类应该存在于同一个文件中。@Rooneyl 的回答显示了如何实施

回答by Master James

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

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

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

添加第二行,其中第一行存在于 system/core/CodeIgniter.php

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

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