Porting PHP Pages to MVC

After several years of hard work in web development, you should have dozens of web pages written in PHP of ASP. In the beginning, every thing seems simple. However, as the number of pages has ever increased, it becomes painful when maintains or changes are required. Eventually, it becomes nightmare whenever changes are required.

Recently, many MVC frameworks have been proposed, which can help engineers to develop and maintain web sites more easily. But, if we want port the ordinary PHP pages to the MVC framework, do we have to rewrite all the current PHP pages from beginning? Or, can we migrate the ordinary PHP pages to the MVC framework incrementally? Of course, we can.

Here, a painless migration approach is introduced. Noticeably, this presented approach only rearranges the original PHP pages according to the structure of MVC framework. Consequently, the resulting programs do not conform with the principles of MVC framework. However, after porting the pages to the MVC framework, you can refine (refactor) the resulting programs whenever you have time. Or, when new functions are needed, they can be developed in the MVC approach.

Here, the proposed approach is described referring to the PHP framework -- Laravel. So, if the web site is developed by using ASP, it has to be transferred to PHP at first. An existed utility, asp2php, can help you transfer a ASP program to PHP. The transferred PHP may need some modifications. For example, the database operations are converted to Mysql functions by default. It could be a good choice to convert the database operations to PDO functions, because Laravel only support PDO interfaces.

Next, let's begin to explain how to rearrange the PHP pages to fit the MVC framework, rather simple.

Suppose that the site has three web pages originally, page1.php, page2.php, and page3.php; and these pages can be visited by the urls, for example, page1.php, as follows

   http://my.net/page1.php

Under the directory controllers of the MVC frame, create a controller, for example, web.php. Next, move all of the original PHP pages to the directory views. Now, all the original PHP programs become the views in the MVC framework, which are dispatched by the actions in web.php. The url for visiting a page, e.g. page1, is as follows,

   http://my.net/index.php/web/page1

In addition, page1 is the default page, and the url http://my.net/index.php/web returns the content of page1.

Here, the url is not rewritten to hide the index.php, seems no body cares that. What I mind more is that the application is maintainable.

How to pass parameters? Just add the parameters after the url, as follows

http://my.net/index.php/web/page1?usr=ajax&role=M

The controller web.php is listed as follows

<?php
class Web_Controller extends Base_Controller {
    // if not login yet, redirect to login form
    function __construct() {
        if (!Session::has("user")) {
            header("Location: ".url("user/login"));
        }
    }
    public function action_index() {
        return Controller::call('web@page1');
    }
    public function action_page1() {
        return View::make('page1');
    }
    public function action_page2() {
        return View::make('page2');
    }

public function action_page3() {

        return View::make('page3');
    }
} // end of class

In this controller, user checking is included, which could be considered to be modified to use the functions provided by the framework. If you do not mind, just keep it. So, every page is modified little and can work normally. In the migration, settings can be put in the configuration files under the directory config. e.g. database.php. In the original PHP programs can get the configurations via the functions provided by Laravel, the following code piece is a function get_conn() for database connection in the original PHP program.

function get_conn() {
    $db_conns = Config::get('database.connections');
    $db = $db_conns[Config::get('database.default')];
   
    $dbsvr = $db['host'];
    $dbnam = $db['database'];
    $dbid  = $db['username'];
    $dbpwd = $db['password'];
    try {
        $conn = new PDO("sqlsrv:server=$dbsvr ; database=$dbnam", $dbid, $dbpwd);
        $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
    } catch(Exception $e) {
        die( '<pre>'.print_r($e->getMessage(), true).'</pre>' );
    }
    return $conn;
}

In summary, the structure of directories is illustrated as follows, (only the mentioned directories are listed)

application
  ├- config
  │    └- database.php
  ├- controllers
  │    └- web.php
  └- views
        └- page1.php, page2.php, page3.php

However, one may think what is the benefit for porting the ordinary web pages to the MVC framework. A primary benefit is that the application can be modified incrementally following the principles of MVC after ported to the MVC framework. For example, the common portion of views can be extracted as a template, or the operations of database can be reimplemented as models.

So, the ordinary web pages have been painlessly ported to the MVC framework; after that, you can refine the application incrementally when you have time or when you feel happy.

Surely, we have to admit that the ported programs do not conform with the separation principles of MVC. Nevertheless, it is good start to let us stop struggling in the tar pits of messy programs. It is our own job and we can decide how to make it more easier for our life.

Good luck, may MVC with you!