Advice/help with MVC please?
Hello I am trying to build a model view controller system and am a little stuck on implementing it.
In my model I have a method in which I pass a query to
Now this does work, but where it's falling down I think is that in my method I am passing in HTML syntax such as <h1> , <p> and class styles, which should only be in the template (view) I think.
Can anybody suggest please how I can pass all of this data to my template (view) without the HTML tags (which i can then put in my view) Thank you.
Indeed, this is a part of the problem with MVC in a template language like PHP.
Logically with an MVC, the view would initiate a request to a controller, which polls a model, which (either) returns to the view or responds to the controller which responds to the view. Languages like PHP are designed to interact at both levels and since it's not an event driven language it becomes a bit deficient with regards to the view -> controller invocation. Stack on that HTTP is stateless, then the combinations become a little weird in an MVC paradigm since you won't really need to go from the view to the controller anymore. So really its more of a controller -> model -> view approach in PHP where the controller controls the majority of the entire system instead of the view initiating all controls.
You could use observable patterns (for which PHP has the built in splObserver and splSubject interfaces, but making your own is probably the most trivial of all design patterns), so that the controller can change things and respond to change type events on the subjects upon receiving notification of said changes. So I'd see a controller as being both an splObserver and splSubject, a model as a splSubject, and a view as a splObserver (or the model and view can also be both). Change in model issues a notify() event, which is picked up by the observer in the controller, which determines which view is in use and set some type of flag which then notifies its observers, which (this is the view now) determines that it's responsible for doing something. The implementation can change of course, you can go view -> controller -> model -> view, or you can go view -> controller -> model -> controller -> view, which is what I would do in a language like Java (not in PHP :P). Both have pros and cons to them, the model -> controller lets me completely decouple views and models so I could link in several views to several models and vice versa without ever needing to modify underlying structure. It does create an absolute mess with collections though, for which model->view is superior in handling.
IMO, this here:
|All times are GMT +1. The time now is 09:47 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.