PHP MVC frameworks VS JavaScript frameworks

AdnanUncategorized8 Comments

Let’s agree that, using PHP MVC frameworks is essential when developing a Web application in most cases. It helps to keep the code organized, structured and easily understood, plus using all the provided functionalities and utilities with the framework. Well, you don’t have to use someone else’s framework as long as you follow the MVC patten when writing your code (separate the View components from the Models and Controllers).

As the term MVC stands, your application must have the Model, View and the Controller components. If you decide to use one of the PHP MVC frameworks out-there, for example: ZEND, Codeignitor and Yii, these components are provided for you and you simply extend and use them for your own purposes.

This is all well and good if your application sticks to the components provided with the framework you use. If for whatever reason, you need to alter the way the framework operates or try to extend its structure to suite your own needs, it might end-up in a mess that makes using the framework part of the problem rather than the solution.

For example, the View aspect of any MVC framework available today for PHP developers has a template tool to help the developer format the data in a suitable html structure. And let me here emphasis the word HTML. Yes, most MVC frameworks stop there. Any javascript required to provide an interactive user experience is now the responsibility of the developer. Well, apart from providing a primitive Ajax functionality to the controllers of the PHP frameworks; there’s very little support to talk of. Some frameworks like Yii, go a little further and provide support for JQuery and encapsulate some of it’s widgets. But again, that does no help when your Web application is highly interactive.

We all know that dynamic and Interactive Web applications are heavy on using JavaScript (Ajax, JQuery etc) to provide a high level of responsiveness when the user interacts with them. Most developers who are accustomed of using PHP MVC frameworks would build the user interface layer of the application in JavaScript and ‘somehow’ interact with it from within their application. But as mentioned earlier this is not supported by the framework and I consider this approach to be a hack and I will tell you why!

If you develop an entire user interface experience using Ajax/JQuery or other JavaScript frameworks while still working with one of these PHP MVC frameworks, you effectively eliminated one component of the MVC term (namely, the View). This actually breaks the structure of your application and cancel the ease of use we talked about earlier, as all your view functionality is now written in Javascript and is not linked to the controller or the model in the way intended by the original MVC framework.

I come to learn this the hard way; after writing few project following this approach (using MVC framework for PHP and using JQuery/Extjs or other JavaScript frameworks for user interface) that you end up with two separate projects, a Javascript project for the front-end and a PHP project for the back-end using your choice of MVC framework.

The conclusion I came to very recently is that, it doesn’t make sense to use an entire PHP MVC framework if you will end up ignoring one of it’s components (the View). If I know for sure that my views are going to be heavily reliant or built entirely from JavaScript frameworks (Extjs, Dojo, JQuery etc) I would have to build my Web application differently and defiantly not with any PHP MVC frameworks mentioned.

My next post will talk about this issue, propose a solution and provide a working example with source code.