intelliproject logo

Location: Web development - PHP    License: The Intelliproject Open License (IPOL)

Object-Oriented PHP development based on the MVC model

Posted by Vagharsh

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.

Skill: Intermediate

Posted: 17/01/2009

Views: 4168

Rating: 4.71 /5

Popularity: 3.98

Sign Up to vote for this article

Introduction

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:

mvc_workflow.png

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.

Constructing an MVC Framework

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.

mvc_block_scheme.png

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

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

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: Armenia
Ocupation: Web programmer
Home page: http://www.tozalakyan.com/

Posted by Constantin Botnari at 18/01/2009 12:37
mm.. very clear and short explanation of how works and how to implement mvc in our php apps.. very useful for me thanks :)
Posted by Anil Kumar at 05/02/2009 23:26
very very thanks ,for your helpful article with nice examples.
Posted by Frederico Mottinha de Figueiredo at 17/02/2009 15:00
Very nice article! Thank you so much, so helpful!

Repeating what Constantin says: "very clear and short explanation"
Posted by Rodel Naz at 21/05/2009 07:15
so far the the best i found about MVC tutorial.

I just would like to ask why Language contains the page title and the hello world.

thanks
Posted by Rodel Naz at 21/05/2009 07:19
i'm new to the MVC templates and oop. i would like to ask how crud is done in this sample.

thanks in advance to anyone kind enough to guide me on this.

Posted by Vagharsh at 22/05/2009 00:56
Rodel Naz, it's just a demonstration of how it is possible to create multi-language template-based applications within MVC framework. You can use whatever you want in your real application's Language file.
Posted by chris at 03/02/2012 17:24
great article, nice use of interfaces; any chance of including: class.DatabaseFactory.php and class.SessionHandler.php - in the zip?
Posted by chris at 03/02/2012 17:25
just found them (in the zip file :)
Posted by dfreeburn at 26/02/2012 12:12
Thanks for this tutorial. It's just what I needed to start learning OOP and MVC in PHP. While developing my first application, I am wondering how best to implement a listing page? For example, my project management app will have classes Client, Project, Task, etc. How do I create the page that will list all the clients and let me choose one to edit? Is this a separate class? or a method of the Application class? What do you recommend? Thanks for your help.
Posted by Vagharsh at 26/02/2012 12:31
gfreeburn, I think you may want to create descendants of basic Model and View classes - e.g. Model_List and View_List. First class will handle db access, pagination and similar tasks and the second one will take care of output.
Posted by dfreeburn at 26/02/2012 12:39
Thanks for the quick response! One other question - what syntax do I use in your template files to access methods from the View_* class?
Posted by Vagharsh at 27/02/2012 05:36
If you are using the structure of the article example, you can simply echo any variable defined in show() method of the View class (or its descendants).

Sign up to post message on the article message board!