Can I apply the MVC Design Pattern to Procedural PHP [closed]
Want to improve this question? Update the question so it can be answ开发者_如何学Cered with facts and citations by editing this post.
Closed 4 years ago.
Improve this questionI've been trying to find out if I can apply the MVC architecture to procedural and how I can go about implementing this into my code. From my understanding MVC basically represents the separation of the business logic, presentational layer and other logic although it always seems to be aimed at OO-PHP in particular.
Can you recommend the best way to approach MVC within a procedural context??
Thanks.
MVC is OO pattern and you want to approach it with a procedural context.
This is plain wrong. MVC has nothing to do with object oriented coding.
MVC is a software architecture pattern which aims at separating the representation of information from the user's interaction with it.
How you achieve that is up to you. You can achieve that using whatever coding form you want, object oriented, procedural, functional, and what not.
With regard to the question at hand: The easiest way to achieve the MVC pattern when you're coding procedural PHP is to use a lot of small functions where each particular function has its own unique task. Don't let a function have many tasks. That way it is more easy to separate things. Also don't keep a lot of functions in the same file. Rather gather related functions together in smaller groups each group in its own file (what actually is done in OO with classes).
Someone does it here with a simple example, MVC without OO: http://www.fluffycat.com/PHP-Design-Patterns/Non-OO-MVC/
Yep, that about sums up MVC... but it doesn't have to be object oriented... you just need to follow a few golden rules:
- The controller receives and processes input, generates any data and puts it in the model.
- The view takes data from the model and renders it.
- The controller should not format data for view - it should have no knowledge of how/why/what the view wants (eg doesn't insert HTML in a text string since the output could be JSON)
- The view should not look-up any data for itself - if it's not in the model then the controller failed in it's job (throw/report an error).
Beyond that you can do things however you want. You'd basically need a set of procedures to act as controllers - parsing $_REQUEST
vars (more likely as GET/POST/COOKIE) performing any data lookup building + filling the model, and then another set of procedures that at as views - taking what's in the model and rendering it for the user. The model can be as simple as an associative array.
Absolutely. Use any combination of the following:
- Use include files.
- Use functions.
- Separate your source files.
- Divide sections of your code within source files.
- Use code blocks (curly braces).
- Separate PHP and/or HTML and/or JS files.
Anything that organizes your code more along the lines of Models, Views, and Controllers. It may not be "pure" or "correct" MVC in some people's minds...but if it organizes your procedural code more meaningfully or teaches you more about MVC, then it's a good thing.
This is kinda weird. MVC is OO pattern and you want to approach it with a procedural context.
The first thing that comes to my mind is to have some kind of templating engine in order to split the PHP from the HTML code. This will be one big step towards procedural MVC :)
The next thing is to name functions of the same group (that means, functions that contains logic for related objects) with prefixes (like you group methods under a class name).
For example - look PHP procedural functions for working with mysql :
mysql_connect()
mysql_real_escape_string();
mysql_select_db();
mysql_query();
etc. And group those functions in separate files. This will help to decouple a bit of the logic too.
If anything else come to my mind, i'll edit my post :)
If you are implementing a new design pattern you most likely are refactoring or writing something from scratch. I highly recommend moving to OOP if you plan to use MVC. It could be implemented procedurally but will be very hackish and not a good solution.
There are several free MVC php solutions that you can pick up easily. Here are a few:
- Zend Framework
- Kohana
- Code Ignighter
精彩评论