Codeigniter MY_Controller:是否只能扩展核心一次?

发布于 2024-12-14 00:55:23 字数 504 浏览 4 评论 0原文

我已经成功地使用 MY_Controller 扩展了核心,如 CI 文档中所述。

这样我可以在 MY_Controller 的构造函数中放置一些重复的函数(即身份验证检查),这样它们总是在我的其他控制器的方法之前运行。

我现在的问题是我的网络应用程序的某些部分是开放的(即不需要登录)而其他部分则需要登录。

因此,我无法从 MY_Controller (其中包含身份验证检查功能)扩展所有控制器。

我想知道是否可以扩展核心以便拥有 LOG_Controller 和 NOLOG_Controller。

然后,需要登录的控制器将从 LOG_Controller 扩展 --- 不需要登录的控制器将从 NOLOG_Controller 扩展。

这可能吗? (或者是形式不好?)

config/config.php 似乎只允许 一个 核心扩展前缀,所以我不确定这是否可能。

让我知道您的想法或者是否有更好的方法。谢谢。

I have successfully extended the core using the MY_Controller as described in CI's documentation.

This way I can put some repetitive functions (ie, auth check) in the constructor of MY_Controller so they always run before methods from my other controllers.

My issue now is that some parts of my webapp are open (ie, don't require login) and others require login.

Therefore, I cannot extend ALL my controllers from MY_Controller (which contains an auth check function).

I wondered if it would be possible to extend the core so as to have, say, LOG_Controller and NOLOG_Controller.

Then, controllers that require login would extend from LOG_Controller --- and controllers that do not require login would extend from NOLOG_Controller.

Is this posssible? (or is it bad form?)

It seems config/config.php only allows for one core extension prefix so I'm not sure it's possible.

Let me know what you think or if there's a better way of doing this. Thanks.

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

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

发布评论

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

评论(5

沩ん囻菔务 2024-12-21 00:55:23

Have a look at this article http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY - It is exactly what you are looking for.

爱情眠于流年 2024-12-21 00:55:23

我使用以下技巧。在 My_Controller.php 中定义所有基本控制器:

<?php

class My_Controller extends CI_Controller{

}

class LOG_Controller extends My_Controller{

} 

class NOLOG_Controller extends My_Controller{

}
?>

这使得所有定义的控制器稍后在您的控制器中可用。只是要避免让基础控制器太胖。

您也可以检查此示例: https://github.com/aquilax/novigeroi2/blob /master/application/core/AQX_Controller.php

I use the following trick. Define all your base controllers in My_Controller.php:

<?php

class My_Controller extends CI_Controller{

}

class LOG_Controller extends My_Controller{

} 

class NOLOG_Controller extends My_Controller{

}
?>

This makes all defined controllers available later in your controllers. Just avoid making the base controllers too fat.

Also you can check this example: https://github.com/aquilax/novigeroi2/blob/master/application/core/AQX_Controller.php

小瓶盖 2024-12-21 00:55:23

由于您尚未发布任何源代码,我们假设您的 MY_Controller 类似于以下示例:

文件路径:application/core/MY_Controller.php

class MY_Controller extends CI_Controller
{
    function __construct() {
        parent::__construct();

        if ( !$this->ion_auth->logged_in() ) {
            redirect('auth/login');
        }
    }
}

您的“受保护”控制器扩展这个并继承它的方法,如下所示:

文件路径:application/controllers/Secure.php

class Secure extends MY_Controller
{
    function __construct() {
        parent::__construct();
    }
}

您希望同时允许 some protected 和 some > 您的控制器中未受保护的控制器 应用。为了让您保护这些控制器,它们必须扩展 MY_Controller 并继承它的 __construct() 方法。对于任何不需要继承该 __construct() 方法的控制器,您只需直接扩展 CI_Controller 即可,如下所示:

文件路径:application/controllers/ Insecure.php

class Insecure extends CI_Controller
{
    function __construct() {
        parent::__construct();
    }
}

就是这样。直接继承(扩展CI_Controller 的控制器将是“不安全的”。

一个考虑因素

这里的主要考虑因素是,您的控制器中可能有其他方法,您希望将其传递给所有控制器 - 在这种情况下,这可能不是您的最佳解决方案。如果是这种情况,您始终可以将这些方法移至帮助程序

Since you haven't posted any source code, let's imagine your MY_Controller looks similar to the following example:

File path: application/core/MY_Controller.php

class MY_Controller extends CI_Controller
{
    function __construct() {
        parent::__construct();

        if ( !$this->ion_auth->logged_in() ) {
            redirect('auth/login');
        }
    }
}

Your "protected" controllers extend this one and inherit it's methods, as follows:

File path: application/controllers/Secure.php

class Secure extends MY_Controller
{
    function __construct() {
        parent::__construct();
    }
}

You want to allow both some protected and some unprotected controllers in your application. In order for you to keep those controllers protected, they must extend MY_Controller and inherit it's __construct() method. For any controller that doesn't need to inherit that __construct() method, you simply extend the CI_Controller directly, as follows:

File path: application/controllers/Insecure.php

class Insecure extends CI_Controller
{
    function __construct() {
        parent::__construct();
    }
}

There you have it. Controllers which inherit directly from (extend) the CI_Controller will be "insecure".

One Consideration

The main consideration here is that you may have additional methods in your controller that you want to pass to all your controllers - in which case this may not be the best solution for you. If that is the case, you can always move those methods to a helper instead.

葬﹪忆之殇 2024-12-21 00:55:23

我认为这是不可能的。有多种方法可以满足您扩展同一控制器的需要。如果请求定向到该文件夹​​,为什么不将所有打开的页面放入一个文件夹(例如 Public)并检查 MY_Controller。如果没有,则需要身份验证。

I think it is not possible. There are ways of doing what you need extending the same controller. Why not put all your open pages in a folder (like Public) and check in MY_Controller if the request is directed to that folder. If not, requires auth.

绝不服输 2024-12-21 00:55:23

另一个简单的解决方案是简单地在 application/controller/my_controller.php 文件底部需要额外的类/控制器

我的自定义控制器看起来像这样,不需要任何额外的配置设置:

class My_Controller extends CI_Controller {

    // do something clever here

}

// do something even more clever here ...
require_once("application/core/tool_controller.php");

对我来说似乎很干燥,然后允许您通过在某处设置模板/布局控制器目录来自定义您的应用程序。我刚刚完成一个较旧的 CI 项目,所以我正在进一步研究它,但我相信你也可以在其他地方使用一些配置变量,使其更加干燥

Another easy solution is to simply require the additional classes/controllers you need at the bottom of your application/controller/my_controller.php file

My custom controller looks like this, didn't require any additional configuration settings:

class My_Controller extends CI_Controller {

    // do something clever here

}

// do something even more clever here ...
require_once("application/core/tool_controller.php");

Seems pretty dry to me and then allows you to customize your application by setting up a templates / layouts controller directory somewhere. I'm just finishing up an older CI project so I'm taking it further but I'm sure you could also play around with some config variables elsewhere to make it even DRY-er

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