codeigniter3 控制器到控制器功能

codeigniter3 controller to controller functions

好的,所以我有页面控制器和 user_authenticator 控制器。

页面控制器就像我的视图的终端,而 user_authenticator 控制器执行与 registration/logging 中的用户相关的功能。

每当我完成 user_authenticator 中的功能时,例如登录,我如何通过页面控制器加载视图?

登录->user_auth(controller)->acc_model(model)->user_auth(controller)->view.

登录->user_auth->acc_model->页面(控制器)->查看。

如果你们能告诉我我正在做的事情是否不切实际并且是更好的做事方式,那对我来说将是一个福音。或者也许我应该坚持在我以前使用的控制器上加载视图。

编辑:所以我可能已经忘记了我的页面控制器的用途,但由于我的迷雾和疲倦的头脑清醒了片刻,我记得了。

我制作了一个页面控制器专门用于加载视图,我想从某种意义上说,页面不会加载所有视图但至少加载大部分视图,例如,如果我有 links对其他观点的看法,我会 link 通过页面。

对于需要特定控制器的特定功能,我想我可以让它们处理加载一些视图。

话又说回来,如果有人能告诉我我在做什么是在浪费时间,应该只删除页面控制器,请告诉我,我想知道为什么。

此外,如果您对我的页面控制器的进一步使用有任何建议,那就太好了!

也关于会话。我有一个基本控制器。

    <?php
class MY_Controller extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }

    public function is_logged_in($data){    
        $session = $this->session->userdata();
        if($session['isloggedin']['username'] == ''){ 
        return isset($session); 
        }else{ 
        return FALSE;}  
    }
}
?>

如何让它自动运行并检查我加载的每个控制器是否设置了任何会话?

我必须把它放到构造函数中吗?还是我必须从所有控制器调用基本控制器方法?

这是最适合您的解决方案。
你可以使用 Hooks
第一步:
application/config/config.php

    $config['enable_hooks'] = TRUE;//enable hook

第 2 步:
application/config/hooks.php

    $is_logged_in= array();
    $is_logged_in['class'] = '';
    $is_logged_in['function'] = 'is_logged_in';//which function will be executed
    $is_logged_in['filename'] = 'is_logged_in.php';//hook file name
    $is_logged_in['filepath'] = 'hooks';//path.. default
    $is_logged_in['params'] = array();
    $hook['post_controller_constructor'][] = $is_logged_in;//here we decare a hook . 
    //the hook will be executed after CI_Controller construct. 
    //(also u can execute it at other time , see the CI document)

第三步: application/hooks/is_logged_in.php //你所关心的

    <?php
    //this function will be called after CI controller construct!
    function is_logged_in(){
        $ci =& get_instance();//here we get the CI super object    
        $session = $ci->session->userdata();//get session
        if($session['isloggedin']){ //if is logged in = true
             $ci->username = 'mike';//do what you want just like in controller.
             //but use $ci instead of $this**
        }else{ 
            //if not loggedin .do anything you want
            redirect('login');//go to login page.
        }  
    }

第四步:application/controller/pages.php

    <?php
    class pages extends CI_Controller{
        function construct ........
        function index(){
            echo $this->username;//it will output 'Mike', what u declared in hook
        }
    }

希望对你有所帮助