Question about building own (PHP) MVC framework
At this time I want to build a MVC framework. Everything is going fine but now I got a problem between controller and view. Hope you can help me.
My MVC is not so different from MVC, it uses the same concept, but technically I got an issue. The question is开发者_JAVA技巧: how to take data from the controller once the view is loaded?
Bear in mind that in views you don't use globals or something like, every parameter (like DB reply, etc) must be passed to the view and the view uses it directly (like... for example, CodeIgniter).
I don't know how to say it better, I'm sorry if you don't understand me. But hope you can!
You probably want to bind a list of variables to the view's namespace.
You could peruse Kohana's View class for some inspiration or ideas.
The point behind the MVC framework is that a View should not need to be aware of the Controller and it's implementation. A View should just have a list of variables that it needs to be present to render correctly, and the Controller implementation worries about making sure those variables are present in the View.
Might I ask why you are designing your own MVC framework though? There are plenty of fantastic solutions out there, I would personally recommend you try out CakePHP, I've used it to ship several commercial products and I swear by it!
You must pass the view a reference to the controller object or the view must use static methods on a related controller class. The view then calls methods on the controller to display results. The view also submits form data to the controller through the controller object or class.
If your View implementation uses a template engine, there is usually a mechanism for adding arbitrary key/value data to the template's scope. If you haven't progressed to that point it's probably worth thinking about a similar strategy.
精彩评论