|
Location: Web development - PHP License: The Intelliproject Open License (IPOL) Object-Oriented PHP development based on the MVC modelPosted by VagharshIn this article we are going to construct a lightweight MVC framework and see how it may be possible to adapt the MVC model to the nature of Web applications. |
Skill: IntermediatePosted: 17/01/2009Views: 4168Rating: 4.71 /5Popularity: 3.98 |
| Sign Up to vote for this article |
MVC (Model-View-Controller) is a popular architectural and design pattern widely used in software engineering. The main aim of the MVC architecture is to separate the business logic and application data from the presentation data to the user. The usage of MVC model in connection with PHP's object-oriented features allows to build scalable, reusable and expressive Web applications.
In this article we are going to construct a lightweight MVC framework and see how it may be possible to adapt the MVC model to the nature of Web applications.
Take a look at the basic workflow of a typical MVC-based Web application:

When a user interacts with a program he(she) is dealing with the Controller. Basically, the Controller is the main start point (the gateway) of an application. It controls the rest workflow based on a particular decision of a user. The Controller should not implement any business logic, it simply dispatches the task request to the Model, which will process the business logic and will return the result data back to the Controller. After having processed results the Controller activates the View, which is responding for the presentation of the data to the user.
If we construct our application on the source code level to follow such kind of structure, we shall be able to control the application's infrastructure, modular business rules and on-screen design separately from each other. As a result the application will become more scalable and more easy to maintain.
Here are the PHP interfaces for the main 3 parts of our application: the Controller, the Model and the View:
./controller/interface.Application.php
./model/interface.Model.php
./view/interface.View.php
The block scheme below shows how the interface implementations will interact each other.

The application startes by executing the init() method. It is the place where we can perform some application-level initialization tasks, e.g. connect the database, start a session, define languages, etc. The main part of the application is executed whithin the run() method. Here we create the Model, check if the user have appropriate permissions for the Model, process the Model and create the View based on processed Model.
The only thing we should do in index.php is to crerate an Application (Controller) object and call it's init() and run() methods.
./index.php
The MyApplication class in the code is a particular descendant of an IApplication implementation.
./controller/class.MyApplication.php
./controller/class.Application.php
We may see 5 predefined initialization methods in our basic Application class. Some or all of these methods may be overrided in the Application descendant (MyApplication class). In addition, if we need more initialization methods we always can override the method init() itself.
./controller/class.Application.php
Look at the static method called buildGoUrl(). This method could be used to costruct URL strings like "http://mysite.com/?go=ModuleName". Draw attention that we have the only index.php which controls the rest of processing tasks, therefore the Controller needs to know "where to go" to dispatch the request to the Model.
In the next 2 listings you can see the settings class which is keeping constant parameters of our application and the MyApplication class (a project-related descendant of the basic Application class) with overriden methods to connect the MySql database, init a database session handler and to define the language options.
./controller/class.Settings.php
./controller/class.MyApplication.php
Now let us digress a little from the Controller and take a look at the Model and View basic classes. Generally, we'll have a separate descendants of these classes for every module (a page, dispatched by the go parameter). For example, if we have a "Contact Us" module in our application, we may have a Model_Contact class (a Model descendant) and a View_Contact class (a View descendant). If there is no appropriate Model or View class for the go parameter, then the basic Model or View classes will be used.
./model/class.Model.php
./model/class.View.php
In this example we are using native PHP templates instead of a specialized template engine like Smarty. Due to a modular system of the MVC model you can easily modify the View to work with any template engine.
./templates/default/tpl.Header.php
./templates/default/tpl.Default.php
./templates/default/tpl.Footer.php
The last thing we shall do is to create instances of appropriate Model and View classes within the run() method of the basic Application class.
./controller/class.Application.php
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Vagharsh
| Experience of developing Web applications using HTML/XHTML, CSS, JavaScript, WML, XML/XSL, PHP and different SQL servers, designing Web databases, creating e-commerce client-server applications and working with Web Services (SOAP, etc.). Experience of client-side development and server interaction using DHTML, AJAX, JSON. Familiar with the basic syntax of XML Schemas and DTD definitions. Intermediate in writing client-side Java applets and creating Flash objects. PHP Skills: Experience of working with PHP versions 4 and 5, knowledge of object-oriented programming principles, design patterns, regular expressions, encryption and security methods, experience of working with GD2, cURL, FTP, PDFlib, Tidy, MING extensions, XML, XSLT, DOM, SAX, SOAP/WDDX interaction experience. JavaScript Libraries: Dojo Toolkit (Dojo Core and Dijit experience), ExtJS (experience of creating interfaces for Adobe AIR applications), jQuery (experience of utilizing and creating extensions), prototype.js and script.aculo.us experience. Location: |
Sign up to post message on the article message board!