tags:

views:

52

answers:

2

Hello,

I'm just learning MVC so you could find my question rather strange...

My Controller have access to different shared objects through Container object passed to Controller's constructor. To access shared objects I should do $this->container->db to access Database adapter or $this->container->memcache to access Memcached adapter. I want to know should I put View object into Container with shared objects or no?

From one side it is really comfortable to take view from this container, but this way I couldn't create multiple Views instances (for example, every time I'm calling Controller's method from View I should have one more View instance). What is the solution? How should I pass View object into Controller and/or how should I create new View instances from Controller?

Thank you!

+1  A: 

If you want that DI experience, do it on views as well, but I don't know if it really helps you anyway. Never call controller methods from views. Instead write some partial view methods and call them from views, which define the page layout (something similar to what Rails does). IMHO if you want to get on MVC gradually, start from core principles and iteratively get to details, but don't learn architectural/design pattern as MVC by parts - architecture, design, the whole matters:)

Gabriel Ščerbák
What if I'd like to use some cached parts of HTML in my view? I have three ways - call controller method, call model method, call cacher method. Calling cacher method means passing some kind of key to it, but both cacher and view don't know anything about keys (but model and controller knows). Calling model method means that we should interact with View object from Model (I don't really know consider it bad or good).
Kirzilla
Saying "cached parts" I meant partial HTML caching.
Kirzilla
A: 

Hmm, maybe try implementing caching for static parts. IMHO try inserting cacher object (through DI) to controller, and let that object decide if you want to send cached partial view or instantiate a new one. If you want to cache data from db, use the same pattern from controller towards models, so whenever in a controller you need models, ask db cacher object (same DI principle). Is it clear enough?

Gabriel Ščerbák
Gabriel Ščerbák, really, sorry for taking so much time. Your answers are really good.
Kirzilla
no problem, but stay focused on the architecture, not concrete implementations, I am not expert, so look out for other implementations, many frameworks do it differently and MVC isn't good for everything (look at GUI frameworks, they have mostly component based structure).
Gabriel Ščerbák