An Introduction to MVC Models with PHP Application Development

As modern PHP application has evolved, many leading developers have adopted a model view controller (MVC) approach to separate front and back-end logic presentation. In web development there are several stages of information presentation – users see a front end design, while your server performs back end calculations and a business logic engine determines how, what and where to present the data. With MVC, these various elements are segmented properly so you can independently work on various aspects of the project.

The architectural setup allows for unique development on the front-end, databases and programming rules of the site. Since teams often work on development projects according to roles, this makes it easier to update, expand or improve upon various aspects of the project. In the view, the front end HTML is delivered via a template (often a Smarty) system, while the controller communicates with the model based upon business rules that determine the most effective way to work on logic operations.  While there are dozens of packages designed for MVC organization in PHP ranging from Cake PHP to Zend, this guide aims to help you understand the architecture of the models and how it affects application development generally.

Web Development with MVC Frameworks

If you have a particular method of developing PHP applications, you may find a MVC frameworks makes the process more efficient. While the framework provides a structure for the process, it still leaves open the specifics according to your requirements. Many of the best practices in MVC also benefit in terms of usability, SEO and load optimization since the framework requires a single index file and properly organizes your directory structure.  While there are a variety of directory structures which will work with the framework here is a common setup:

/home
Index.php
/app
/control
/include
/model
/view

Likely, you’re already using some version of this structure already, so this only formalizes development best practices.  Within your index file you’ll define your site path which ensures delivery of the application follows the back end structure:

$path = path(dirname(_file_));
Define (‘path’, $path);

An initiation script within your index file will also lead your controller, template, models and logic. By automatically loading the classes, you can ensure independent operation of the various aspects of your web app. While a full explanation of each aspect of MVC is beyond the scope of this introduction, we’ll analyze the role of the model which processes logic for the application.

The model is used to process, store and route data from your mySQL database. With a unique set of functions that determine database operations, you can easily extend, expand and update the data segments of your website with relative ease.  With a global set of variables that call the database you can load the data for presentation, while allowing you to update your models without impairing the front end. Modern application development often involves adding new data tables, importing data and expanding database functions – with MVC you can work on a new release point while also keeping your design and front end independent.

Links

1 response so far ↓

avatar

1. Response by : PHP Application Development on Nov 29, 2010 at 1:41 am

Hi There,

This opens up a new world of possibilities for application development inside corporations. Now application developers can blend the capabilities of the PHP and .NET worlds

Thanks,
Mick

Leave a Comment