Welcome Guest, Not a member yet? Register   Sign In
Login System - Code Igniter just makes things excessively complex..
#1

[eluser]tomclowes[/eluser]
I am trying to make a login system for my website.

From the Login perspective.. I have no problems.

When the user has successfully logged in I:

Code:
$data = array(
        'username'  => $vars['1'],
        'logged_in'  => TRUE
        );

        $this->session->set_userdata($data);


My question relates to using this 'logged in' user in practice.

I have a header view file which needs to display whether the user is logged in, and if so their details. Thus in every controller I will have to call a model which gets this data, and then pass it to my header view - lots of effort.

In addition to this, many of my pages need to know if a user is logged in and get specific information. For example if a user is logged in, one page needs to get the users predictions and display them.

Not everything is user related. Some pages have user data on them as well as other general data collected from a database.

Creating models to collect all this data and then passing it in one massive array to my view is going to be completely ridiculous.

Have I missed the point?
Thanks
#2

[eluser]WanWizard[/eluser]
For stuff you need in every controller, create a MY_Controller, put it in there, and then have your controllers extend MY_Controller instead of Controller.
#3

[eluser]tomclowes[/eluser]
Right, So in MY_Controller I can do all the login checks, and get the user details so in each individual controller it is as simple as calling the view with a particular $var.

Thanks for that.

What about my query about getting dynamic content to the view file. User dependent or not.. getting it all and sending it in one massive array is more effort than its worth..
Thanks
#4

[eluser]Vheissu[/eluser]
I tend to create a class for admin users and a class for client users inside of my MY_Controller. So I have the following.

Code:
class MY_Controller extends Controller {

    function MY_Controller () {
        parent::Controller();
    }

}

class Admin_Controller extends MY_Controller {

    function Admin_Controller() {
        parent::MY_Controller();
    }

}



class Client_Controller extends MY_Controller {

    function Client_Controller() {
        parent::MY_Controller();
    }

}

Then inside of your normal controllers extend whichever class you want to extend. So if you have a controller for admin stuff you would obviously extend the Admin_Controller, client stuff would extend the Client_Controller and other operations like some simple login pages and other controllers that don't require any verification.
#5

[eluser]tomclowes[/eluser]
I appreciate the suggestion, but the pages in question are for both logged in and not logged in users.

Simply if they are logged in, a lot of dynamically loaded content is displayed.

My main query now is as to how one is meant to simply and efficiently display lots of information from a database (some of which is specific to a given user) in a view page.

Thanks
#6

[eluser]WanWizard[/eluser]
That doesn't matter.

Just add code to the constructor of the MY_Controller to do the preparation, like retrieve the logged-in state from the session, store it somewhere, etc. You can add a is_logged_in() method to your MY_Controller.

Then, in the constructor of your controller, call the parent contructor first. After that, you can call $this->is_logged_in() to fetch the logged in state, and act upon it (p.e. redirect or show an error if the user is not logged in or has no access). Then, in your controller method, use the same method to selectively load data into the view array. You can put the result in the view array as well, so you can have "if logged in then" sections in your view file.
#7

[eluser]tomclowes[/eluser]
Yes - I understand the basic premise of the suggestion.

One page for example needs to display a list of a users posts, and a number of replies. The underneath it shows the top 10 posters.

So yes I can use this is_logged_in() method and then call a model which gets the users posts, and the number of replies in a long complex array. Then I have to call another model which gets the top ten posters, returns an array, then I have to add to two arrays together, send them to the view, and somehow efficiently output this complex mess of an array.

My question is.. is this the right idea. Is this what one is meant to do with CodeIgniter?
If so why is it so stupidly complex such that MVC has essentially lost its benefit.

Thanks
#8

[eluser]WanWizard[/eluser]
Yes, that's the way it works, and no, this is not complex.

One way or another you have to gather all information to be displayed in the page. With basically means running a query, looping through the result, echo out HTML in the process.

Why is putting this query in a model, and the HTML producing loop in a view, so much more complicated? The result is a much tidier flow, and clean and modular code, a lot easier to maintain.

It's not more difficult then
Code:
$data = array();
if ( $this->is_logged_in() )
{
    $data['posts'] = $this->posts_model->get_posts();
    $data['topten'] = $this->posts_model->get_top_ten();
}
else
{
    // make sure your loops don't break
    $data['posts'] = array();
    $data['topten'] = array();
}

$this->load->view('my_page', $data);
#9

[eluser]tomclowes[/eluser]
Many Thanks - I am getting my head around this (gradually).

WanWizard - thanks for the code. When presented like that it does seem pretty simple.

So in MY_Controller I have:

Code:
<?php

class  MY_Controller  extends  Controller  
{

    function MY_Controller ()  
    {
        parent::Controller();
        
    }
        
        
    function is_logged_in()
    {
        $session_id = $this->session->userdata('session_id');
    

        if($this->session->userdata('logged_in') == TRUE)
        {    
        
        return true;
    
        }
        else
        {
        return false;
        }
    

    }
}


My only slight confusion is this bit:

Code:
function MY_Controller ()  
    {
        parent::Controller();
        
    }

is the construct right.. (PHP 5 style) which loads all the properties of CodeIgniter?


So.. I have a function which checks log in.

Then in each of my controllers I place the following:

Code:
if ($this->is_logged_in())
    {
        $this->load->model('user_model');
        $data['user']=$this->user_model->user_details();

    }

$data is then passed to my header view.

That works great - of course the assumption is that I place the above in each individual controller.


On some pages however I want to output a username in the footer view or main page body view. Is there anyway of defining an array variable $user in MY_Controller which can simply be accessed in any view without anything having to be passed?

Thanks for all the help.
#10

[eluser]pickupman[/eluser]
You are on the right track. You may want to alter to look like this
Code:
class MY_Controller extends Controller  
{
    public $data; //create data property

    public function __construct()  //php5 style
    {
        parent::Controller();

        
        if($this->session->userdata('logged_in') == TRUE)
        {    
          $this->load->model('user_model'); //load model
          $this->data['user'] = $this->user_model->user_details();  //get user details

        }else{
          $this->data['user'] = FALSE;
        }
        $this->load->vars($this->data); //Make $this->data available to all views / controllers
    
}

Check out the link on Base controllers in my signature. It's the same example you are looking for.




Theme © iAndrew 2016 - Forum software by © MyBB