Welcome Guest, Not a member yet? Register   Sign In
New to CI
#1

[eluser]drewbee[/eluser]
Hi all,

In my search for a framework I have finally come across CI. I previously used cake -- and it seemed to 'locked down'. Everything seemed black and white with it. I am already liking CI and how it loosely handles everything.

I do have a few starting questions.

Session management -- the package available within CI is an absolute no no. Whats up with storing the data in a cookie?
Regardless, I tend to prefer using a custom session handler which works with the database. This runs transparently in the background, and as such I still access my session data through the $_SESSION vars. Is there any reason I should not follow this practice within CI?

I was basically just thinking of creating it as a library, and loading this library on every page that I need to work with sessions. If you have any advice of things I should avoid or 'should do instead'... let me know.


MVC Logic -- pages.
I am having some problems understanding the MVC logic when it comes to where everything 'goes'. Please take the following example.
Lets say I have a registration form and for this form I need to verify that the 'username' does not already exist in the database.

If I understand this correctly, it would be as follows (psudo code):
model:
function usernameExists($username)
SELECT username FROM accounts WHERE username = $username;
if rows > 0
return false;
else
return true;
end if;

controller:
load (validation)
$rules['username'] = 'required|usernameExists';
set_validation_rules($rules);

function callback_usernameExists($username)
{
return model::usernameExists($username);
}



Sorry about the psudo code, and please ignore the syntax (I can gurentee its not exactly what its suppose to be, hehe). I hope the idea is there though.

Basically, the model is the one to do the database interactions, so the controller is the one who must talk to the model which returns its findings to the controller so the controller can return the models findings to the validation routine (false makes it fail).

Thanks for the assistance and would appreciate some feedback.
#2

[eluser]Michael Wales[/eluser]
Your understanding of the MVC concept is spot on - you could even take it a bit further by extending the validation class with an if_exists() function or working on some code to move validations into the model, or completely new files (my personal preference) called validators. All of these are advanced options and things to look at down the road - I would stick to the basics for now, which it seems like you have a firm grasp of.

As for the way CI handles sessions - there are quite a few session libraries out there, all of them really great. I highly recommend you look at DBSession, sounds like what you need - of course, you could always write your own library as well. Nothing wrong with that at all.
#3

[eluser]drewbee[/eluser]
Awesome. I setup DB Session as well as my database configuration and put them both in the autoload configuration.

The data is correctly populating in the table and all that jazz so it does look good to go. One thing I will probobly do slightly different is create an account_id field within the session table. I tend to like retrieving all relative user data along with the session data (and not store there account-specific data in the session vars).

This way I dont have to have two queries to get there information (session retrieval then account information retrieval), but rather just all comes from the session retrieval. Personal preference I guess.

Once I make those modifications to it, my next little quest will be on to designing a auth system around this and learning the database class. I have always used mysqli in the past, and looking over the class most of it seems to act the same way.

Once that is setup, everything else should be gravy.

Oh, I assume the controller does the auth as well?

Oh, I really like the idea of the extension of the validation class with the if_exists...
I am thinking something along these lines
if_exists($tablename, $column, $value) // Select 1 FROM $tablename WHERE $column = $value;
#4

[eluser]drewbee[/eluser]
I'm mad now. Why did no one tell me about CI Before? Sad lol.

controller:
Code:
<?php

class Registration extends Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library(array('template','validation'));
        
        $this->template->title = 'Seller Registration';
        $this->template->meta_robots = 'noindex,nofollow';
        $this->template->addCrumb('/registration/index.html', 'Registration');
        $this->template->addTemplate('registration');
        
        $rules['email'] = 'trim|required|max_length[30]|valid_email|callback_emailUnique';
        $rules['first_name'] = 'trim|required|min_length[1]|max_length[50]';
        $rules['middle_initial'] = 'trim|exact_length[1]|alpha';
        $rules['last_name'] = 'trim|required|min_length[1]|max_length[50]';
        $rules['username'] = 'trim|required|min_length[5]|max_length[30]|callback_usernameUnique';
        $rules['password_1'] = 'required|matches[password_2]|min_length[5]|max_length[30]';
        $rules['password_2'] = '';

        $this->validation->set_rules($rules);
        
        $fields['email'] = '';
        $fields['first_name'] = '';
        $fields['middle_initial'] = '';
        $fields['last_name'] = '';
        $fields['username'] = '';
        $fields['password_1'] = '';
        $fields['password_2'] = '';
        
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->template->draw();
        }
        else
        {
            $links = array(anchor('validate_email', 'Validate Email'), anchor('home', 'Home'));
            $this->template->draw('You have succesfully registered an account', $links);
        }
    }
    
    function usernameUnique($username)
    {
        if ($this->validation->if_exists('accounts', 'username', $username))
        {
            $this->validation->set_message('usernameUnique', 'Username is taken. Please try something new.');
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function emailUnique($email)
    {
        if ($this->validation->if_exists('accounts', 'email', $email))
        {
            $this->validation->set_message('emailUnique', 'Email Address Already Exists.');
            return false;
        }
        else
        {
            return true;
        }    
    }
}
?>

Validation:
Code:
<?php
class CIEXT_Validation extends CI_Validation
{

    function __construct()
    {
        parent::__construct();
        $this->set_error_delimiters('<span class="error">', '</span>');
    }
    
    function if_exists($table, $column, $value)
    {
        $query = $this->CI->db->query("SELECT        1
                                       FROM          $table
                                       WHERE         $column = ".$this->CI->db->escape($value)."");
                                  
        if ($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }    
}
?&gt;

Already had to design myself a nice little template library as well. To cool. Big Grin Big Grin Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB