Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been developing a web application using CodeIgniter.

I've also implemented all the functions following the MVC structure, but I am not sure whether I've confused myself so far...

If I use the functions from the model in the view, is that violating the MVC pattern? Is this considered as the view talking to the model?

Let me show some codes:

One from my controller:

public function loader($page){
    #load pages if these settings will be loaded for sure
        if ( ! defined('BASEPATH')) exit('No direct script access allowed');

        $this->load->helper('url');
        $this->load->library('session');
        $this->load->model('app_model');
        $this->load->model('app_user_model');
        $this->load->view('app/header');
        $this->load->view($page);
        $this->load->view('app/footer');
    }

One from my model:

public function submit_login($username, $pass){
    //try to login, if the login credentials are correct, set the user session to username.

        $this->db->select("name, PersonName");
        $this->db->where("name", $username);
        $this->db->where("pass", $pass);
        $result = $this->db->get("members");


        if ($result->num_rows() > 0){
            foreach($result->result() as $user){
                $this->session->set_userdata('user_name', $user->PersonName);
                $this->session->set_userdata('user', $user->name);
            }
            return 1;
        }return 0;
    }

Partial view:

$is_login = $this->app_user_model->is_loggedin();

if($is_login){
    //if user is logged in, prompt to log out
    echo "Welcome back ".$this->session->userdata['user_name'].".";
    echo anchor(base_url('profile/logout'), "logout" );
    echo "<br/>";
}else{

    echo validation_errors();
    //if user is not logged in, ask to log in first
    echo form_open('profile/login');

    $user_name = array(
                'maxlength'   => '25',
                'size'        => '55',
                'style'       => 'width:30%; margin-left:5%',
                'name'        => 'username',
                );

    //get the date 
    echo "<div class='user_name_input'>";
    echo "Username(email)";
    echo form_input($user_name, '', 'class="user_name"');
    echo "</div>";

In this case, I called is_loggedin() from the user_model..

Am I following the MVC pattern correctly?

Thanks for helping.

share|improve this question
2  
Somebody will tell you that its against MVC others will tell you the opposite. Practice show me that in views there must be only simple logic (if/else), loops, escapes but no requests to your DB (including using a model to do that).. Its better to gather and pass any variable information from your controller to your view.. – Svetlio Apr 24 at 13:48

1 Answer

up vote 3 down vote accepted

led not to be judgemental, but I think you are missing the point of a MVC.

The url calls the relevant controller, the controller calls model layer(s) to fetch data and determine view and then calls the correct view.

So with your case you call the login controller, which calls a model to check the login and from there it either displays the logout button or the errors.

I am sure you can use it your way around, but, my opinion, that is the incorrect way of using it.

share|improve this answer
i guess you are right... – led Apr 24 at 14:00
No worries man, it takes a while to get use to the flow of a MVC, at least you are asking the right questions and noticing that you are not using it correctly. – We0 Apr 24 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.