CodeIgniter Forums
Confused by senior developer by talk of objects in a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Confused by senior developer by talk of objects in a view (/thread-32942.html)



Confused by senior developer by talk of objects in a view - El Forum - 08-10-2010

[eluser]sico87[/eluser]
Hi There,

I have started a new project at work today and my senior developer has asked me to look into changing the way I work and implementing objects into my views to I can do checks within my view using the object.

To me this seems wrong, as surely this is the work of the Model and/or controller? Or am I wrong? I assume he is wanting me to implement something like this?

Code:
<?php if($user->can()) : ?>
Hello You can do this
<? endif; ?>

$user would be the object.

Is this possible? Is it the right way to do things? How would I do this?


Confused by senior developer by talk of objects in a view - El Forum - 08-10-2010

[eluser]intractve[/eluser]
Its most certainly possible. I use it often.
CI is a very loosely coupled MVC so you can use Database calls $this->db->get() even in your views!
Its upto you to decide how you want to code using CI


Confused by senior developer by talk of objects in a view - El Forum - 08-10-2010

[eluser]mddd[/eluser]
I think you have to separate form from function here.

The MVC principle means that you try to keep the 'smart bits' of your website (checking if a user is logged in, getting things from the database etc) out of your views. The model is the place to do those things. The view is simply for displaying things.

This is different than saying you should not use objects in your view. There's nothing wrong with that. Just don't be tempted to do complicated things to those objects. If you need to save a password for a user, do it in the model. But if you pass a "user" object from the controller to the view, it's fine to use $user->is_logged_in() to check if the user is logged in. That is not really any different from doing " if ($user_logged_in) { ... } ".


Confused by senior developer by talk of objects in a view - El Forum - 08-10-2010

[eluser]fMertins[/eluser]
[quote author="mddd" date="1281466111"]I think you have to separate form from function here.

The MVC principle means that you try to keep the 'smart bits' of your website (checking if a user is logged in, getting things from the database etc) out of your views. The model is the place to do those things. The view is simply for displaying things.

This is different than saying you should not use objects in your view. There's nothing wrong with that. Just don't be tempted to do complicated things to those objects. If you need to save a password for a user, do it in the model. But if you pass a "user" object from the controller to the view, it's fine to use $user->is_logged_in() to check if the user is logged in. That is not really any different from doing " if ($user_logged_in) { ... } ".[/quote]

Hi everyone,

I agree, I always try to make my views as simple as possible, with the complexity of things at controllers and models. The most information ready to show in view, better. The only exception in my projects are session information, because they are already in session, then isnĀ“t necessary to pass they to the view. And yes you can have a any kind of variable at view that is an object but avoid to call database things directly from view :-) Hope can help, good luck!


Confused by senior developer by talk of objects in a view - El Forum - 08-10-2010

[eluser]Josh K[/eluser]
That's fine. In fact that is often used to hide or show information depending on the users "roles" or abilities.