Thoughts On PHP MVC Web Frameworks

I recently started working on a project for work where they use Microsoft Severs. After some consideration I decided to used the ASP.NET MVC framework instead of just going with the .NET web froms framework since I had some experience with MVC frameworks in PHP. Going from PHP to a language like C# was a huge jump. C# (like Java) is a very strict language which heavily focuses on using the object oriented design to solve problems. First off,  PHP  really only has one data structure which is kind of a hybrid between an array and a dictionary. This suits well when dealing with small tasks like handling user input but when you need a more complex data structure PHP falls a little short. Another leap was solving problems in a object oriented fashion. Here is an example of code that I wrote a little over a year ago. This is not uncommon for  PHP  …

<?php
include 'functions.php';
password_protect();
echo_header();

if (!isset($_GET['runId'])){
die("This is an error! Please go back to the home page!");
}

if (is_admin($_SESSION['runnerId'], $_SESSION['password'])){
$runQuery = mysql_query("SELECT * FROM runs WHERE id='" . mysql_real_escape_string($_GET['runId']) . "'");
}
else{
$runQuery = mysql_query("SELECT * FROM runs WHERE id='" . mysql_real_escape_string($_GET['runId']) . "' AND runner='" . $_SESSION['runnerId'] . "'");
}

if (mysql_num_rows($runQuery) == 0){
die("An error occured. Please contact the site admin!");
}


$run = mysql_fetch_array($runQuery);
?>
<div class="sidebar2">
Name: <?php echo stripslashes($run['name']); ?> <br>
Date: <?php echo $run['date']; ?> <br>
Time: <?php echo convert_seconds_to_time_format($run['seconds']); ?> <br>
Distance: <?php echo $run['distance']; ?> <br>
Type: <?php echo $run['type']; ?><br>
Pace : <?php echo calculate_pace($run['seconds'], $run['distance']); ?> <br>
Location: <?php echo stripslashes($run['location']); ?> <br>
Comments: <?php echo stripslashes($run['comments']); ?> <br>
</div>
<?php
echo_footer();
?>

As you can see it is very C-ish. No objects only static helper functions like  convert_seconds_to_time_format() or echo_header(). Queries are hard-coded into each php file and the result is an array. View rendering is mixed with logic. While this code is straight forward for small functions to understand it is almost impossible to maintain. I have seen  PHP and Asp files that contain one method that spans over 1,000 lines long.

MVC

When first introduced to the MVC framework Codeigniter I dismissed it. I thought that it was over complicating the problem. But as the projects became harder I soon realized that a web framework would be need if you wanted to do anything productive. After about a month of coding I was begin to master the framework. The above code was simplified and turned into the following …
function display ($id = NULL)
{
//ID wasn't set so show error
if (!isset($id))
{
show_404();
return;
}

//Get the run data
$this->db->select('*');
$this->db->from('runs');
$this->db->where('id', $id);
$run_query = $this->db->get();

//No run exists
if ($run_query->num_rows() == 0)
{
show_404();
return;
}

$run = $run_query->row();

//Security check
if ($this->session->userdata('runnerId') != $run->runner)
{
echo "You do not have permission to view this!";
return;
}

$this->template->write_view('content', 'run/display', array('run' => $run));
$this->template->render();
}

As you can see the intent is becoming more clear. However if I were to write another action there would be alot of simpler steps. Making sure an id was passed into the method. Making sure its not null. Then making sure that the id references a row in the database and making sure the user has permission to view the row …. Seems like a lot of boilerplate code before we can even get to the main part of the controller method which is to load an object from the database and call a view to render it. Another problem with this setup is that our controller is passing our model from the database into the view. Why is this a problem? Well lets say we need to display something a little more complicated. Like run has a Datetime field. In order to properly render it our view would have to convert the Datetime into something that is human readable. Is that the view’s job or the controller’s? Technically we want to keep the view as dumb (least amount of logic) as possible. The solution (which I will talk about in my next blog post) is strongly-typed view models!

Leave a comment

Your email address will not be published. Required fields are marked *