简体   繁体   English

Code Igniter 2 - 控制器/方法覆盖

[英]Code Igniter 2 - Controller/Method override

If a user is not logged in I would like to override the existing Controller and call the auth/login one instead keeping the browser URI in tact.如果用户未登录,我想覆盖现有的 Controller 并调用 auth/login 来保持浏览器 URI 完好无损。 (Every user has to be logged in to use the site) (每个用户都必须登录才能使用该网站)

I have tried a pre controller hook, but its too early.我已经尝试过一个 pre controller 钩子,但它为时过早。 The auth library has not been instantiated by this point.此时尚未实例化 auth 库。 The post controller hook is too late as the target controller has been instantiated.由于目标 controller 已被实例化,因此 controller 后挂钩为时已晚。

Editing the constructor of the library seems pointless also, as it has already been created.. So i am a little stuck..编辑库的构造函数似乎也毫无意义,因为它已经被创建了..所以我有点卡住了..

Any ideas please?请问有什么想法吗? :).. :)..

Thanks谢谢

I would override the default controller and extend in each controller.我会覆盖默认的 controller 并在每个 controller 中扩展。 Create a new file: ./app/core/MY_Controller.php with the next code:使用以下代码创建一个新文件:./app/core/MY_Controller.php:

class Secure_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();

        /* replace this code with yours */
        if( !$this->session->userdata('logged_in') ) {
            redirect(base_url() . 'login', 'refresh'); // your login url
        }
    }
}

Then, use this code in each controller that you should be accessible by logged users然后,在登录用户应该可以访问的每个 controller 中使用此代码

class Main extends Secure_Controller {

Have you considered just saving the URI to flashdata, redirecting the visitor to another controller (login page) and then just putting the referrer into a hidden form field?您是否考虑过将 URI 保存到 flashdata,将访问者重定向到另一个 controller(登录页面),然后将引用者放入隐藏的表单字段? I'm doing the same thing with one of my sites using Codeigniter and it works fine.我正在使用 Codeigniter 对我的一个站点做同样的事情,它工作正常。 The only difference is that the URI is not intact during this process.唯一的区别是在这个过程中 URI 不是完整的。

Perhaps the answer to this question will be the same for you?也许这个问题的答案对你来说是一样的? CodeIgniter only allow access to certain controllers when logged in CodeIgniter 仅允许登录时访问某些控制器

(If so, don't forget to replace the "function className()" by "function __construct()" and "parent::Controller();" by "parent::__construct();") (如果是这样,不要忘记将“function className()”替换为“function __construct()”和“parent::Controller();”替换为“parent::__construct();”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM