【问题标题】:CI Borrow other controller's function in another controllerCI 在另一个控制器中借用其他控制器的功能
【发布时间】:2009-07-28 00:08:19
【问题描述】:

我的前端有一个控制器,我正在使用 DX Auth 库。

我想使用 DX Auth 的注册,但将其包含在我的首页控制器中...我可以简单地复制粘贴该功能,但有更好的方法吗?

【问题讨论】:

    标签: codeigniter controller


    【解决方案1】:

    您是否尝试在您的首页控制器中登录和注册用户?您需要根据installation instructions 安装DX Auth,并查阅手册中的一些示例和功能参考。

    您需要在构造函数中加载 DX Auth 库:

    class Auth extends Controller  
    {  
        function Auth()  
        {  
            parent::Controller();  
            // Load library  
            $this->load->library('DX_Auth');
            $this->load->library('Form_validation');
        }
    
        // implement other login functions like the examples
        // using the library:
    
        function login()  
        {
            if (!$this->dx_auth->is_logged_in()) { 
                $is_valid = $this->form_validation->run('login');
                $username = $this->input->post('username');
                $password = $this->input->post('password');
    
                if ($is_valid && $this->dx_auth->login($username, $password)) {
                    // redirect somewhere
                } else {
                    // show some errors
                }
            }
        }
    
        // other authentication functions
    }
    

    如果您愿意,您可以创建一个助手来保存您的身份验证功能,以便您可以从任何控制器访问它们。按照安装说明设置您的数据库并进行一些基本的用户注册和登录工作——它们相当全面。

    【讨论】:

    • 我是个菜鸟,所以 MVC 的东西才刚刚开始流行……这是有道理的。谢谢!
    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多