Friday, May 6, 2011

An MVC simplified architecture combined with web services

This web 2.0 application architecture works with a non-monolithic controller and data model. That is they are split among multiple files and classes just like views and web services:

controller  -------------- >  model
^                 (3)             ^
.                                 |
. (1)                         (3) |
.                                 |
view   ----------------- >  service

PHP include only. Sets template variables. Can silently perform some business logic actions too.
AJAX call. These calls will be available for integration purposes as well.
PHP include followed by API calls.

Line (1) is dotted to point out that although it is the view that calls for the controller, usually it doesn't need to pass any specific input data. COOKIES/GET/POST data are grabbed by the
controller autonomously since they are superglobal arrays in PHP. Therefore a view usually only includes the respective controller in order to carry out a form submission and/or get its template variables. Indeed a view is just a template written in plain PHP.

A view can include a controller on the server-side and/or use a web service on the client side (in JavaScript). Views are either HTML or PHP files. In the latter case they usually contain only a controller include statement at the beginning and some embedded PHP code that implements the server-side part of the presentation logic only.

This model securely suits Web 2.0 developments where business logic is split among client and server. It was inspired by a much-discussed Rasmus Lerdorf's article. There is no involved framework that implements this architecture, it's all standard plain PHP and judicious use of the file system.

View, model or web services can be as complex as you want, e.g. support themes, use advanced JavaScript libraries, SOAP or provide a full database abstraction.

Since this model is based upon web services, your web application will be fully interoperable from the outset, without any additional complexity or code duplication. The same ideas can be implemented in any HTML-embedded language, not only PHP.

Found here a sample PHP implementation about collecting people's contacts. To simplify matters, the following sensible choices have been made for implementing each part of the model:

JSON web services
the Prototype library to provide some HTML extensions (DHTML menu, client-side form sanitizing and validation, calendar widget, cross-browser AJAX support); PHP itself as a template language
PHP Data Objects, an abstraction layer for data-access only and prepared SQL queries.
the PHP standard data-filtering functions, for added security

For more details please read PROJECT/NOTES.txt inside the people.tgz tarball.

No comments: