Saturday, January 09th, 2010
MVC is not a new concept. It is considered by some to the evolution of IPO (Input, Processing, Output), which was the best-practice model applied to the linear, text-only applications. Since PHP is simple and not type specific, it can be easily abused to the point at which applications become unmaintainable. This is mainly because of combining model, vie and controller into a single PHP script. In a proper MVC application, there are no scripts, only components.
How Not to Do It
Ask any inexperienced PHP developer to create a small login or guestbook application and he/ she will probably come up with a single file. The file would be called login.php or guestbook.php and would handle both displaying, adding, validations etc. This approach has number of problems, quite apart from using GET instead of POST to effect a change in a database.
Instead, take a look at a more mature approach to development that allows you to follow the MVC design.
An MVC Approach
The first rule of MVC in PHP is to split up your files. There are number of possible approaches, but many favor taking the step of actually using different file extensions to represent different roles in the MVC pattern and then using require_once at the appropriate time to link together.
If you are unsure as to what code should appear in which modules, you can rely on a few rules to ensure that you’re following this methodology:
i. .php (control page) should never contain SQL queries or HTML.
ii. .phpm (classes) should never contain HTML
iii. .phtml (templates) should never contain SQL quries, and only very basic PHP (for/if/while).
MVC in Symfony Framework
If you are used to developing PHP websites without a framework, you probably use the one PHP file per HTML page paradigm. These PHP files probably contain the same kind of structure: initialization and global configuration, business logic related to the requested page, database records fetching, and finally HTML code that builds the page.
You may use a templating engine to separate the logic from the HTML. Perhaps you use a database abstraction layer to separate model interaction from business logic. But most of the time, you end up with a lot of code that is a nightmare to maintain. It was fast to build, but over time, it’s more and more difficult to make changes, especially because nobody except you understands how it is built and how it works.
As with every problem, there are nice solutions. For web development, the most common solution for organizing your code nowadays is the MVC design pattern. In short, the MVC design pattern defines a way to organize your code according to its nature. This pattern separates the code into three layers:
The Model layer defines the business logic (the database belongs to this layer). You already know that symfony stores all the classes and files related to the Model in the lib/model/ directory.
The View is what the user interacts with (a template engine is part of this layer). In symfony, the View layer is mainly made of PHP templates. They are stored in various templates/ directories.
The Controller is a piece of code that calls the Model to get some data that it passes to the View for rendering to the client. When we installed symfony the first day, we saw that all requests are managed by front controllers (index.php and frontend_dev.php). These front controllers delegate the real work to actions.
Developers, try to implement your code in MVC structure even though its small application, to make your code clean and manageable. PHP is a great and simple language, but as a developer we need to make our code with all the major standards, security stuffs with good architecture to make our PHP language proud and also it gives more satisfaction to us.
Enjoy Good Coding