使用离子AUTH认证为codeigniter在另一个控制器 [英] use ion auth authentication for codeigniter in another controller

查看:128
本文介绍了使用离子AUTH认证为codeigniter在另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与codeigniter的Web应用程序。我已经安装了离子验证我的身份验证模式。

I am trying to build a web application with codeigniter. I have installed Ion Auth as my authentication model.

默认Auth.php控制器对用户进行认证,并建立会话。

The default Auth.php controller authenticates the user and sets up the session.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller {



    function __construct()
    {
        parent::__construct();
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->helper('url');

        $data['title']="Login Page";
        $this->load->view("view_site_header",$data);

        // Load MongoDB library instead of native db driver if required
        $this->config->item('use_mongodb', 'ion_auth') ?
        $this->load->library('mongo_db') :

        $this->load->database();    

        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
    }

    //redirect if needed, otherwise display the user list
    function index()
    {
        // if not logged in - go to home page
        if (!$this->ion_auth->logged_in())
        {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }
        // if user is an admin go to this page
        elseif ($this->ion_auth->is_admin())
        {
            // if an admin, go to admin area

            //set the flash data error message if there is one
            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

            //list the users
            $this->data['users'] = $this->ion_auth->users()->result();
            foreach ($this->data['users'] as $k => $user)
            {
                $this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
            }

            $this->_render_page('auth/view_users', $this->data);                
        }   else
    {
        //redirect them to the default home page 
        $data['title']="IMS Home Page";
        $this->load->view("generic/view_site_header",$data);
        $this->load->view("generic/view_generic_nav");
        $this->load->view("generic/view_content_generic");
        $this->load->view("view_site_footer");
    }
}

我想要做的就是创建我的应用程序逻辑的一个新的控制器和离开AUTH控制器进行身份验证。

what I want to do is create a new controller for my application logic and leave the auth controller for authentication.

我怎么可以使用AUTH控制器,以确保我的用户登录访问我的新控制器时?另外我需要的信息,分裂国家将提供给新的控制器。

How can I make use of the auth controller to ensure my user is logged in when accessing my new controller? in addition I need the ession information to be available to the new controller.

我新的控制器,master_data具有以下code:

my new controller, master_data has the following code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Masterdata extends CI_Controller{

    function index ()
    {
            $data['title']="Master Data Home Page";
            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_home");
            $this->load->view("master_data/view_master_data_footer");

            echo $this->session->userdata('username');



    }
}

显然回声$这个 - &GT;会话级&GT;用户数据(用户名); 作为新的控制器没有在auth控制器会话知识不起作用

obviously the echo $this->session->userdata('username'); does not work as the new controller has no knowledge of the auth controller session.

pciated一如既往的帮助AP $ P $。

any help appreciated as always.

亲切的问候,

推荐答案

首先自动加载ion_auth库。
如果妳只是要检查,如果用户登录的,只是检查它在每个控制器的构造函数加载ü

First autoload the ion_auth library. If u simply want to check if the user is logged-in, just check it in every controller's constructor u load

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

    if (!$this->ion_auth->logged_in()) {
       // redirect to login view
    } 
}

如果妳刚好有多个组,U可以建立内部应用程序/核心/ MY_controller.This控制器新的控制器将检查用户是否登录in.You可以扩展主控制器上创建新的controller.A很好的解释这是由大卫john.Check这给出链接

这篇关于使用离子AUTH认证为codeigniter在另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆