Welcome Guest, Not a member yet? Register   Sign In
Controllers and Models, what goes where...
#1

[eluser]zlatiborac[/eluser]
I know this is the newbie question of the decade, but I don't count my self in the newbie category and still have difficulties in understanding what goes where.
Until the early morning hours last night I used models only for work with database, querying-collecting-working with data from databases. But then, since I am in one big project of mine, while talking with one more experienced developer I realized that something in my logic is wrong. I heavily rely on Controller to do all the work (meaning preparing all the data for View to display it), but when I saw him doing some stuff I noticed that 80% of work I did in Controller he did in Model.

Here is one method from the Controller
Code:
function doLogin(){
        //uchitaj potrebne biblioteke
        $this->load->library('encrypt');
        $this->load->library('form_validation');

        //prihvat podataka sa forme
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        //hass lozinke
        $passHashed = dohash($password);

        //query nad tabelom users
        $query = $this->db->query("SELECT * FROM users WHERE username = '$username' AND password = '$passHashed'");

        //proveriti da li postoji user i pass
        if($query->num_rows() !=0){
            $this->session->set_userdata('loggedin', 'yes');
            $this->session->set_userdata('name', $username);
            redirect ('admin_auth/loggedIn');
        }
        else
        {
            redirect ('/');
        }
    }


function isLoggedIn(){
        if($this->session->userdata('loggedin') == 'yes'){
            //$this->loggedIn();
            return TRUE;
        }
        else
        {
            redirect ('/');
        }
    }
Now I will dislocate that SQL query in my Model, but, he is moving isLoggedIn also to a Model.
I need help on this. I am afraid that I will get stuck later on in my work and all this work, time and effort will go into vain Sad
#2

[eluser]Buso[/eluser]
It's ok to have most of the work done in the models, but this isn't black and white. isLoggedIn could go in your controller, in your model, or even in a library.

I rather having a library for auth since in my case, it interacts with many other components, and it gives me another layer of abstraction when I use it along with its model, but that's just how I liked it. In any other case, I put everything inside a single model or library.

Try to make your controllers as tiny as possible, so you can follow up the application flow by just taking a look at it. That means for example, that you don't need to know the exact implementation of isLoggedIn, better hide it inside a model or library.
#3

[eluser]jedd[/eluser]
Whereas I'd have is_logged_in() within [url="/wiki/MY_Controller"]MY_Controller[/url].

Much of this is down to taste & personal preference. I think so long as you're thoughtful with your (future) design requirements and consistent with your practices, you will be fine (or, to be more accurate, at least as fine as you can be when trying to meet a moving target).
#4

[eluser]zlatiborac[/eluser]
@Buso
Yes, you are right that not all is black and white and that there are shady areas Smile
After reading more about whole MVC concept a little (lot) bit more I realize that Controller should be thing that is only used to connect Views and Models together. To say it simple, user wants something in View, View than contacts Controller which than asks Model for information (data), and when it gets that data it then serves it back to View.
And in this last sections lies my confusion. Should the Model do all the work (calculations, database querying, checks....) and Controller then just serves that to View or the Controller is doing also some of the job? This is the thing that makes me confused.

@jeed
I agree with you about personal preferences and taste but am I right to say that if you don't start your job and do it by the rules wouldn't it be harder to finish that work and after that almost impossible to maintain that product and upgrade it?
#5

[eluser]jedd[/eluser]
[quote author="zlatiborac" date="1277593407"]
Should the Model do all the work (calculations, database querying, checks....) and Controller then just serves that to View or the Controller is doing also some of the job? This is the thing that makes me confused.
[/quote]

The question often comes down to separation of business logic from (mere) data handling.

Quote:I agree with you about personal preferences and taste but am I right to say that if you don't start your job and do it by the rules wouldn't it be harder to finish that work and after that almost impossible to maintain that product and upgrade it?

Correct - but the same can be said for any programming journey - if you get it wrong up front (the design phase) then you're going to get bitten in all kinds of interesting ways later on.

It's a nice idea but people's requirements often change savagely between initial spec and first release, and often all over again during the life of the system.

I think the point, buried within my pithy aphorisms, is that you're likely going to be stuffed no matter what you do. Smile
#6

[eluser]Buso[/eluser]
I have been told that in MVC, the controller isn't supposed to be a connector between the views and the models, they only choose what to do, which page to serve. Then that page (view) can call the models/libraries (maybe through a second view layer, which make those calls) it needs.
But it seems to be that in CI it's better to use it as a connector, and I find it more comfortable.

Another thing you can philosophise about is whether you must have big models like Auth, or many smaller models (one per table) like Users, Roles, Resources, etc. I prefer the first option right now, but I'm changing my style all the time so I couldn't tell what's better.




Theme © iAndrew 2016 - Forum software by © MyBB