Welcome Guest, Not a member yet? Register   Sign In
__construct my bad code with Ion Auth
#1

(This post was last modified: 07-19-2016, 03:57 PM by Sarog.)

Good day
I use authorization with Ion Auth. Registraition work and all good, but my code is so bad. I use it each controller in order to understand whether the user is registered. 

PHP Code:
    private $logged_in;
    private 
$login;

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

        
$this->load->helper('cookie');
        
$this->load->model('login_model');

        if(
$this->session->userdata('logged_in')){
            
$this->logged_in true;
            
$this->login $this->session->userdata('login');
        }
        else{
            if(
get_cookie('identity') && get_cookie('remember_code')){
                if(
$this->login_model->autorize_cookie(get_cookie('identity'),get_cookie('remember_code'))){
                    
$this->logged_in true;
                    
$this->login get_cookie('identity');
                }
                else{
                    
$this->logged_in false;
                    
$this->login '';
                }
            }
            else{
                
$this->logged_in false;
                
$this->login '';
            }
        }
    }
    public function 
show() //for example
    
{
        
$CI = & get_instance ();
        
$CI->load->view('includes/head');

        if(
$this->logged_in){
            
$CI->load->view('reg',array('login'=>$this->login,'head_name'=>'Проекты'));
            
$CI->load->view('project',array('login'=>$this->login));
        }
        else{
            
$CI->load->view('login');
        }
    } 
Please, help me, how i can to improve this. Thanks)
Reply
#2

I believe ION Auth comes with a MY_Controller you can add other classes to the MY_Controller and add your code to it a new Controller class.
Just extend the MY_Controller to your new Controller.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

I'm sorry. for understanding how it works, I took only some functions in Ion Auth/
I don't see MY_Controller
Reply
#4

Ion Auth uses the Auth Controller what I am saying is to create a MY_Controller you can find that in the Users Guide, then create your own Controller and a dd the methods you need to it. You could even place the auth controller in the MY_Controller class.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(07-19-2016, 04:20 PM)Sarog Wrote: I'm sorry. for understanding how it works, I took only some functions in Ion Auth/
I don't see MY_Controller

use MY_Controller as the base class and extends all your controller from it.the code which is required in all controller goes into MY_controller and you don't have to copy paste in each controller.

some common usages:
1)authentication code.
2)loading common libraries , helper or any other resources
3)enable disable profiler or caching
4) simple template system etc

google or search on git hub . you will find some good examples.
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull
Reply
#6

(07-19-2016, 03:56 PM)Sarog Wrote: Good day
I use authorization with Ion Auth. Registraition work and all good, but my code is so bad. I use it each controller in order to understand whether the user is registered. 

PHP Code:
    private $logged_in;
    private 
$login;

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

        
$this->load->helper('cookie');
        
$this->load->model('login_model');

        if(
$this->session->userdata('logged_in')){
            
$this->logged_in true;
            
$this->login $this->session->userdata('login');
        }
        else{
            if(
get_cookie('identity') && get_cookie('remember_code')){
                if(
$this->login_model->autorize_cookie(get_cookie('identity'),get_cookie('remember_code'))){
                    
$this->logged_in true;
                    
$this->login get_cookie('identity');
                }
                else{
                    
$this->logged_in false;
                    
$this->login '';
                }
            }
            else{
                
$this->logged_in false;
                
$this->login '';
            }
        }
    }
    public function 
show() //for example
    
{
        
$CI = & get_instance ();
        
$CI->load->view('includes/head');

        if(
$this->logged_in){
            
$CI->load->view('reg',array('login'=>$this->login,'head_name'=>'Проекты'));
            
$CI->load->view('project',array('login'=>$this->login));
        }
        else{
            
$CI->load->view('login');
        }
    } 
Please, help me, how i can to improve this. Thanks)

First of all... that is a really bad code... Why are you calling a CI instance inside the controller? Isn't it already called?

Anyway... Besides pointing you to the official documentation which is more than great, I can also refer you to a series about Ion_auth (on my own blog...): http://avenir.ro/authentication-system-w...h-and-ci3/

In there you also have a few more tutorials about working with CodeIgniter.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB