Welcome Guest, Not a member yet? Register   Sign In
MVC question (view-controller relationship)
#1

[eluser]bookvir[/eluser]
Hi all,

I got an MVC question - I want to know how I am supposed to do something according to MVC architecture and I'm not sure if I'm doing this right.

I'm using a View consisting of some mini views:
- header
- menu
- login form
- page data
- footer

When a user is logged in the overall View is the same, only parts change (*):
- header
- menu *
- login information *
- page data
- footer

Am I supposed to check whether the user has logged in in the controller and then choose an appropriate view (eg. login form - login information)? It implies that i have to use the same code in every controller which uses the same page template (and since roughly the only part that does change in my controllers is really "page data", that would be a lot):

Code:
class A extends Controller {

    /* ... */
    
    function stuff()
    {
        /* ... */
        
        $this->load->view('header');
        $this->load->view('menu');
        if ($this->session->userdata('logged_in')) {
            $this->load->view('logininfo');
        }
        else {
            $this->load->view('loginform');
        }
        $this->load->view('pagedata');
        $this->load->view('footer');
    }
}

My current solution is quite different and I suppose it's not really nice. It just doesn't feel nice. I think.

In the controller I just load all the views:

Code:
function stuff()
{
    /* ... */
    
    $this->load->view('header');
    $this->load->view('menu');
    $this->load->view('form');
    $this->load->view('pagedata');
    $this->load->view('footer');
}

Now in the view "form" I do something (ugly?) like this:

Code:
<?php if ($this->session->userdata('logged_in')) { ?>

    CONTENT OF 'LOGIN INFORMATION'

<?php
}
else {
?>

    CONTENT OF 'LOGIN FORM'
    
<?php
}
?>

So, is this wrong? It just seems a simpler solution, having less code (no repeated code in controllers) at the price of "cheating within views". I mean, is this okay with the MVC pattern?

Thanks a lot.


Messages In This Thread
MVC question (view-controller relationship) - by El Forum - 09-07-2009, 08:00 PM
MVC question (view-controller relationship) - by El Forum - 09-07-2009, 08:13 PM
MVC question (view-controller relationship) - by El Forum - 09-08-2009, 01:57 AM
MVC question (view-controller relationship) - by El Forum - 09-08-2009, 02:22 AM
MVC question (view-controller relationship) - by El Forum - 09-08-2009, 03:30 AM
MVC question (view-controller relationship) - by El Forum - 09-09-2009, 04:05 AM
MVC question (view-controller relationship) - by El Forum - 09-09-2009, 05:15 AM
MVC question (view-controller relationship) - by El Forum - 09-09-2009, 08:52 AM
MVC question (view-controller relationship) - by El Forum - 09-09-2009, 06:03 PM
MVC question (view-controller relationship) - by El Forum - 09-09-2009, 08:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB