CodeIgniter Forums
Controllers and Views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Controllers and Views (/showthread.php?tid=29615)



Controllers and Views - El Forum - 04-15-2010

[eluser]bastones[/eluser]
Hi,

I'm new to CodeIgniter and I have a question about controllers and views. It's took a bit of time to understand the relationship with MVC - specifically between controllers and views and that the controllers do the logic, and views find out the result of that logic and display text, images and data in the respective View file. I just want to make sure I have understood this correctly so I have one question:

If I have a controller and a method that is a "logged in" section of a site, I'd first need to make sure they're logged in - and the problem is how to check the logic in the Controller while displaying the relevant output in the View file. So how I'd do it is by checking if they're logged in, and if they are, return a boolean value of 1 using the second parameter when loading the View file into the method - and same applies if they're not logged in except return boolean 0 and then check these variables values in the View file to return logged in/not logged in information.

Would this be a correct way of doing it including in other different scenarios or is there a better way? Just want to make sure I've got this MVC thing properly.

Thanks!


Controllers and Views - El Forum - 04-15-2010

[eluser]gvillavizar[/eluser]
[quote author="bastones" date="1271361637"]Hi,

I'm new to CodeIgniter and I have a question about controllers and views. It's took a bit of time to understand the relationship with MVC - specifically between controllers and views and that the controllers do the logic, and views find out the result of that logic and display text, images and data in the respective View file. I just want to make sure I have understood this correctly so I have one question:

If I have a controller and a method that is a "logged in" section of a site, I'd first need to make sure they're logged in - and the problem is how to check the logic in the Controller while displaying the relevant output in the View file. So how I'd do it is by checking if they're logged in, and if they are, return a boolean value of 1 using the second parameter when loading the View file into the method - and same applies if they're not logged in except return boolean 0 and then check these variables values in the View file to return logged in/not logged in information.

Would this be a correct way of doing it including in other different scenarios or is there a better way? Just want to make sure I've got this MVC thing properly.

Thanks![/quote]

Hello,

This will depend on if your view file displays the same content when they are logged in or out. If they don't, then you better check in the controller if they are logged in, and if they are then load the view file that displays the logged in data. Else, load the view file that displays the logged out data. This is the most logical approach to MVC (IMO).

In case you have a view file that displays similar data to both states and a section that displays limited content to logged in users, then you can pass either a boolean or a variable to the view and then condition the view part to display one content or another.