PHP MVC
PHP MVC - what is it? why would you want to use it and which one, and finally, how? I'll skipp over the acronym explanation and i'll godirectly to "Why MVC?" more exactly "why PHP MVC?" although the idea can be extended to other development languages.
You need to work with MVC (PHP) if ....
- You have a enough experience with PHP and you're aware of everything new in each version of PHP as of PHP 5 (at least superficially)
- If you're tired of copying the same HTML to each page, then when it comes to change something at the design or html you have to update all of the 1000 pages of your site
- you're tired when you implement a code from a previous site to another site, modifying dozens of lines of code to achieve the desired result.
- When you have to change a simple SQL query you see the HTML and PHP, a tiring thing.
- You want to start being a serious web developer
- You want to pull money from web development
- Want to make a form of the HTML and validate it without writing even a bit of HTML (unless you know better you're special like me, and you have to take care of the CSS) without making 1000Ifs order to detect potential attacks and block them, with both display and input validation being on the same page and also you don't want to waste time populating it with the same mistakes every time the values have been sent by the user, annoying him and making him run from your website because you didn't have even the willingness to save the values that he entered in the fields (yeah yeah, you heard right, there something that will ease your work)
- You want the PHP code separate from HTML in a more organized manner than you could create it from 0
- You want to make applications or websites that are successfully validated XHTML or W3Validator
- You want the best search engines to see that your sites use valid XHTML and HTML not a simple, ugly and hard to read HTML.
- You want to get used to the way in which you are required to work when you go to work as a webdeveloper
- You want everyone to be able to understand your code , because anything written in pure PHP could be done in away by a developer and in another way by another developer, and sometimes the code isn't that simple to understand if only both developers would use the rules of a MVC
- Are you tired to write bunch HTML + PHP + MySQL lines when you want do a simple thing like a login
- ... and again - if you serve PHP in the morning, serve PHP at lunch , in the evening PHP, in the night and between meals you serve PHP
Ok, I know that the last line scared many of you BUT THIS WAS NOT the goal. The idea is that you can't directly grab a MVC without having more than basic knowledge of programming language in question.
Therefore, we conclude that an MVC ...
- It lets you do more with less code as a simple programmer (although behind the scenes are tons of lines of code, that aren't that irrelevant to the developer, at least not as long as the programmer wants more from the MVC )
- was designed precisely to increase production and maintenance of applications in a very short time
- it's more easily to break it in pieces, take these pieces and implement them elsewhere saving valuable time
- It is very useful when several developers create MVC-style applications
- what the user sees is completely separate from data processing and storing information
Thus we come to explain the acronym MVC.
MVC = Model View Controller. Although many definitions are somewhat more complex on the internet, I will define them as I think it's better, a development overall which, at the programming level, what the user sees is completely separate from the processing of information and the location of this information .
In other words, when you want to change the way the user information is displayed i'll go in VIEW (here we're talking about basic simple HTML sites, which include simple PHP - echo-s). When I want to change the way the input of the user it's processed i'll go in CONTROLLER. And finally, data access is done through the model.
In other words ...
- VIEW = what the user sees
- Controller = how it came to what the user sees (the processes – based or not on the input /information of the user's identity - from behind VIEW)
- MODEL = dealing with the reading/writing information in a storage medium (most often here we refer to a database, but can also be a source file or any other existing storage place defined by the programmer)
Before you start
I said that it's not ok to have 1000 pages that implement the same layout, that is copied to each of these pages, being very annoying when you want to modify the page design (which may seem unusual for some, to change the design).
There are many who use a master page. That is the file index.php and the rest of the pages are indexed, so look like links http://example.com/index.php?page=contact. If you have not worked with master page then you better do some google-ing about it (PHP master page) before you take on a MVC, largely because this version is closer to this style of working.
Fears of begginers are unfounded
At first, working with a PHP MVC can give the false impression that what you do in such a MVC is much harder than you do it in a simple file php that because the code is divided into several files and - believe me or not - makes sense (I remember that from the beginning i was trying to find all sorts of bugs in the MVC style simply because it seemed hard, but it's like Brancusi - "Things are not difficult. Difficult is to put yourself in the position to do them. ")!
Another thing that may scare beginners is that it works very much with classes. Well, everything is related to the MVC's OOP (object oriented programming), ie object-oriented programming. While I worked through VB and C++ and even with classes and objects in PHP, I tell you I was afraid since I learned that most of the things within the PHP MVC is closely related to classes and objects. Even if you're not a big expert in PHP classes and objects, you learn in a forced way about them, and believe me or not you even get to like them – anyway there are essential and incredibly useful.
Zend MVC
The MVC PHP site i recommend and use for approx. 2 weeks and i think i'll be working with it for a long time from now on is Zend MVC. Zend MVC uses the Zend Framework. This framework is, in short, a library of PHP classes that implement them in order to "achieve" Zend MVC . Basically, the Zend MVC is a set of folders and files, through which the objects of classes in the Zend Framework are initiated , so that a programmer, taking account of several conventions and appointment sharing files/ classes / methods defined for it, can begin to build your own application.
... that's all for starters. I am convinced that you did not find much help. Moreover, I didn't give any example, but this post is an indication that the material is good to read it after tasting the examples ... so everything is clear, slowly but sure.
Good luck and i'll be waiting for you ...
There is a similar article, click here for list.
| There are no comments on this article. Be the first to say your opinion. |






