Welcome Guest, Not a member yet? Register   Sign In
Regarding OOP and MVC
#1

[eluser]greedyh4mster[/eluser]
Hiya everyone!

I just started using Codeigniter. There are a few concepts here that I have seen, through the codes of other Codeigniter developers. Below are several questions that I have. Hopefully, someone will help me out.

1)Regarding MVC.
I understand that the Model is a place where you put your database related functions. Views is a place where you will put the HTML. Controller is where you put the 'checking of user input', and redirecting codes.

For example (I am trying to build my own cms):

Member_Model.php:
Code:
class Account_model extends Model{
    function logout($user_id){
        if($this->db->update('user', array('token_cookie' => ''), array('user_id' => $user_id))){
            return TRUE;
        }
        return FALSE;
    }
}

view_members.php:
Code:
<div class="sidebar">
   <div class="logout">&lt;?php echo anchor('member/logout','Logout'); ?&gt;</div>
</div>

Member_Controller.php:
Code:
class Account extends Controller{
    function logout(){
        if($this->member_model->logout($user_id)){
            set_cookie('rememberme', '', 1);
            $this->session->sess_destroy();
            //redirect('user/login');        
        }
    }
}

Just to ensure that I have the right concept. The above code is correct right?
The controller should not contains any 'database related process' and should only have 'codes that are related to the process' right?

Following on my concept, I thought of the OOP. In codeigniter, the controller acts or is itself a 'webpage' where the user's input will go towards to. If that is the case, wouldnt be much more 'OOP' if I were to put my 'Member object' into a library instead of doing it in a controller?

I have checked out several cms here and realized that most of them have the common concept of: controller->library->model->View

So if I am to put my Member object into a library. Wouldn't it be better if I have the member library to handle the member objects, including 'database related process'. So the concept will end up like: controller->library->view

Then again. The Model will seem useless in the above concept, isnt it? But in the above concept, I assume that a library should be self-sufficient enough to handle an object (in my case: Members) which means a library should contains 'database related process' as well.

I understand that it is a bit confusing. but I will seriously require some help in clearing my doubts over these. Please kindly help me.

Thanks!!
#2

[eluser]InsiteFX[/eluser]
You are correct, but you may also want to look at the MY_Controller
in the users guide.

The MY_Controller will allow you to use code that you would need in
Every Controller that you write say for example Login and Logout code.
Then extend all your Controllers from MY_Controller.

Enjoy
InsiteFX
#3

[eluser]greedyh4mster[/eluser]
Thanks for the reply, InsiteFX.

Hmmm. By the means of "You are correct", you are referring to my MVC concept? Or the latter, OOP concept?

After reading through my post again, I found that I did not specify my question. =S

My question will be: Which concept should I follow? The MVC or the OOP? Both are stated as above.

If I extend MY_Controller, it will goes more towards MVC right? Because if we are talking about objects. I have a User object and a Member object. A user is able to register/login. While a Member is able to logout/change password. By putting the login/logout into the MY_Controller, then the OOP wouldnt make sense anymore right?
#4

[eluser]markup2go[/eluser]
Hi there greedy,

I think it comes down to personal preference and the specific situations you may run into. The user guide even states "Models are optionally available for those who want to use a more traditional MVC approach.", so I would say to use whichever style works for you, in your situation.
#5

[eluser]pjturpeau[/eluser]
[quote author="greedyh4mster" date="1269797430"]
I have checked out several cms here and realized that most of them have the common concept of: controller->library->model->View

So if I am to put my Member object into a library. Wouldn't it be better if I have the member library to handle the member objects, including 'database related process'. So the concept will end up like: controller->library->view

Then again. The Model will seem useless in the above concept, isnt it?
[/quote]

This is the approach I try to have most of the time. However, the model is not useless, since it could be used by several different libraries which may act as plugins to a main one (for example).




Theme © iAndrew 2016 - Forum software by © MyBB