• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.

#2
[eluser]wiredesignz[/eluser]
Look into using the MY_Controller extension and placing all common methods and content into it.

#3
[eluser]bookvir[/eluser]
[quote author="wiredesignz" date="1252394023"]Look into using the MY_Controller extension and placing all common methods and content into it.[/quote]

Thanks a lot. I didn't think of that. Smile

#4
[eluser]The Mask[/eluser]
Is it right that you're loading many views from your controller? I would have used include files and only load one view.

#5
[eluser]n0xie[/eluser]
[quote author="The Mask" date="1252416172"]Is it right that you're loading many views from your controller? I would have used include files and only load one view.[/quote]
It's what is generally revered to as 'partials'. You load some part of the page with its corresponding view. You usually catch the output into a string and then pass it to a 'template' view, but outputting it directly can work as well (although I imagine it is less flexible since you have to load the views in order).

For example: if you have a dynamic sidebar menu, and you want to change the way it looks, you only have to change the partial view for the sidebar. It makes maintaining a large scale website very easy.

#6
[eluser]bookvir[/eluser]
[quote author="The Mask" date="1252416172"]Is it right that you're loading many views from your controller? I would have used include files and only load one view.[/quote]

It was just an example to make my point. Most likely I'd do something like this instead:

Code:
$data['menu'] = $this->load->view('menu', '', TRUE);
$data['pagetext'] = $this->load->view('text', '', TRUE);
/* ... */

$this->load->view('template', $data);

See CI's reference. Including files within a view is an option if you wish - however it's best to leave loading views to the controllers, I think.

#7
[eluser]wiredesignz[/eluser]
Loading views in controllers? Yes, I think so too.

Try this instead.
Code:
$this->load->vars('menu', $this->load->view('menu', '', TRUE));

$this->load->view('template');

#8
[eluser]BrianDHall[/eluser]
Directly to the thread topic, MVC methodology does not forbid use of logic in the view - infact, that's why CodeIgniter supports both it's own template engine and pure PHP code inside Views, even ensuring that $this in views refers to the codeigniter object itself.

So using "if logged in display this, if not display this" is acceptable. An option is to just use an if/then include when you are loading your views, so "if logged in load this view, if not load this one"...but personally on my login menu sometimes I have three or four options depending on the permissions of the person logged in, but I only need this in one view file in one part of the entire program and its just for convenience navigation.

It would just be a bigger hassle for me, I find, to go into MY_Controller for special view logic, or doing alternate view files just for getting a minor piece of navigation working.

Its good to know how to avoid it though, just incase it does become a big deal, or at some point you are required to use views differently. Just one more tool to put in the tool bag...but you don't have to use it if its overkill. Sometimes the flathead screwdriver fits the phillips-head hole Smile

#9
[eluser]wiredesignz[/eluser]
[quote author="BrianDHall" date="1252525937"]... even ensuring that $this in views refers to the codeigniter object itself...[/quote]

$this in views, refers to the loader object, not the controller (codeigniter) object. The controller class variables are assigned to the loader during view rendering.

#10
[eluser]BrianDHall[/eluser]
[quote author="wiredesignz" date="1252558987"][quote author="BrianDHall" date="1252525937"]... even ensuring that $this in views refers to the codeigniter object itself...[/quote]

$this in views, refers to the loader object, not the controller (codeigniter) object. The controller class variables are assigned to the loader during view rendering.[/quote]

I actually found this out the hardway myself today, LOL. I could have sworn it worked differently, but it must have been a false memory.

Tu che!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.