简体   繁体   中英

How to use ion_aut library in codeigniter with MY_Controller

Hi all i'm searching for a usage of IonAuth library for Codeigniter 2.x and i'm curious to learn how it works.

Downloading the package i have a controller auth.php. i had a look at this http://blog.nexico.net/article/secure-area-codeigniter-framework-and-ion-auth

So i create, in the core folder of CI, a MY_Controller. But how can i use it?

Working with ion_auth.php library is pretty easy. You need to follow these steps. I assume you have followed ion_auth installation process.

First you need to create a MY_Controller class and put it in core

Class MY_Controller Extends CI_Controller{
    public function __construct(){
        parent::__construct();
        if (!$this->ion_auth->logged_in()) 
        {
            redirect(site_url('auth/login'));
        }
    }
}

Make sure you autoload the ion_auth library. The second important thing is that you need to extend your every controller with MY_Controller.
(Note : if you dont want to extend with MY_Controller but want to use simple controllers that extend CI_Controller put the above condition in every controller's constructor) If user is not logged in and try to access any page he will be redirected to auth/login.

Another useful function is here.

$user   =    $this->ion_auth->user()->row();

This will return the logged in user information.

Hope that's all you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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