I want to be clear on this one. I am using codeigniter.
What/Which is the better/proper/standard/most appreciated way of OOP design in codeigniter in building web applications?
Generally,
I. Creating a custom class like User and have a login function? Like,
class User{
var $db, $id, $username, $password, $role; //have all user attributes here
function __construct() {
//instance for accessing the database
$this->db =& get_instance();
}
public function login(){
//do the login here
$this->db->load->model('user_model');
$success = $this->db->user_model->login($this->username, $this->password);
//isn't this redundant using models? or should I rename the model function name like checkLogin?
return $success;
}
//other class functions
}
OR
II. Directly calling a login function in the model from the controller without creating a user object?
O_o
In scheme 1, I am making the model to query ONLY in the database.
I can also make ‘User’ object to be inherited like for ‘Client’, ‘Admin’ and add specific functions for each object. I have the advantage to make the CI controller to focus in the layout and passing of variables to the view, which is primarily its purpose(not quite sure) and let the my custom classes/objects tackle operations in my web app.
BUT i feel a redundancy in my login function where I called another login function in the model. Is this good?
While in scheme 2, is it proper to let my controllers handle all operations like login, register, or create a log AND receiving input posts and passing of variables to the view?
I really need some guide.
My OO skills needs to grow.
Thank you so much!
Firstly I think it would be useful for you to read this arrticle:
Access Control List
I’m working on a project now that uses CodeIgniter and the approach that we are using here maybe will suit you too. If you need to check for authorisation, I think the best place to do this is in the constructor of the class with calling a login function if the user is not registered yet. But it is a little bit general approach so if you have classes where some functions should be available only to registered users then you should make the check inside the function.