<<  back to Blogs page

MVC Frameworks

Intro

Finding the right MVC framework for web application development is very much a matter of personal choice.

The following notes are now very much out of date (all at least 5 years old - and counting). I ended up writing my own minimalist PHP Framework (see PIP framework documentation).

Laravel

Before spending any time building my own PHP / PDO framework - I thought I should just check to see if there were any new kids on the block.

Thats when I discovered Laravel.

Documentation

Laravel Laravel seems to take the best of Ruby on Rails, Groovy and Grails and other PHP frameworks. And simply does it better - or you could say - it does it better - simply!

If you go and take a quick look at the Laravel website; the first thing you will notice is the excellent documentation. A simple description of each function and comprehensive API.

What is missing for me is a sample application or some tutorials. So what I plan to do here is describe building my first simple Laravel application.

Installation and Configuration

Download the latest version from laravel.com and follow the instructions.

Config

To test that everything is working you only need to change one line in the /application/config/application.php file.

Just set the url to your site path, mine is:

	
'url' => 'http://localhost/laravel1/public',

Just open your browser and navigate to the application home page, and you should be up and running.

Views

Standard layout

It is easy to set up a template, so that your views only contain the information and data relevant to the specific page.

more ...

Forms

Is it better to embed the PHP in the HTML or embed the HTML in the PHP?

In the past I have always tried to make sure that php files containing HTML are primarily HTML with small injections of PHP, thus:

<p>
  <label for="email">Email:</label>
  <input type="text" name="email" id="email"
           class="<?php echo $person->hasError('email');?>"
           value="<?php echo $person->get('email'); ?>"/>
  <span class="error">
    <?php echo $person->hasError('email', true); ?>
  </span>
</p>

My first attempt with Laravel looked something like this:

<p>
  <?php echo Form::label('surname', 'Surname'); ?>
  <?php echo Form::text('surname', $user->surname); ?>
  <span class='error'>
   <?php echo $user->hasError('surname'); ?>
  </span>
</p>

But because Laravel has such an elegant syntax for expressing form components I have changed my forms to look like this:

<?php
  // Surname
  echo '<p>'.Form::label('surname', 'Surname');
  echo Form::text('surname', $user->surname);
  echo '<span class="error">'.
    $user->hasError('surname').
    '</span></p>';
?>

I think this is much easier to read, and allows the use of standard PHP constructs within the code. It means that my non-form views are different in that they are mainly HTML with small bits of PHP, but for forms, I prefer this approach. What do you think?

Getting Laravel running

A slight problem with One.com hosting is that they do not have a /public_html folder, so everything in the root structure is public. So I needed to change the directory structure (see below) and change the .htaccess file to read:

 RewriteRule ^(.*)$ index.php?/$1 [L]

(add question mark [?] after index.php) instead of

 RewriteRule ^(.*)$ index.php/$1 [L]

Laravel 3.2 Requirments

When I went to upgrade to Laravel 3.2 (about a month ago) I found that it now requires the PHP mcrypt library. A quick look at the install procedure for mcrypt and I decided to carry on using Laravel version 3.1.8.

But before I go live, I would like to move to the latest version of Laravel, and also make sure that my development system is using the same version of PHP as the live version.

I am using Mac OS X 10.6 (Snow Leopard) and I found a very simple upgrade procedure at php-osx.liip.ch/. I copied the one-liner command for PHP 5.3 in the terminal window (and waited about 5 minutes while it worked its magic) - then phpinfo() confirmed that I was running PHP 5.3.13 with mcrypt installed. Two birds, one stone.

A quick test with the latest version of Laravel and everything is looking OK.

Laravel Directory Structure

When I first discovered Laravel the directory structure was beautifully simple - just three folders: Application, Laravel and Public. With Laravel version 3.2.1 there are now 5 folders and the paths.php file, so I want to simplify the structure, where the Public files sit in the root directory and everything else is tucked away in one folder.

My first step is to create a new folder (which I call core) and copy all files apart from the public folder into the “core” folder. I move all the files from the public folder into the root. The only two changes required is update the path to paths.php in index.php (root folder) and update the url in the /config/application.php file.

That was too easy. A few tests are required to see if it all works before I upload the app ...

Groovy and Grails

Having just bought a new Mac Book I thought I would document the steps for installing Groovy and Grails.

Step 1 - Download Groovy

Go to http://groovy.codehaus.org/Downloadand download Groovy (Version 1.5.7). The Tutorial 1 - Getting Started is a help but as a newcomer to Mac OS-X I found the installation instructions more than a bit vague.

Download the zipped Binary Release and copy the resulting folder (mine was called groovy-1.5.7) to your target folder. I stored mine in the Library folder (under Macintosh HD). This is where the Java folder resides, so it seemed a reasonable place to store it.

Step 2 - Download Grails

While we are here, we might as well do the same for Grails via the http://grails.org/Download download website and download the Binary ZIP. Move the resulting folder (grails-1.0.3) to the Library folder.

Step 3 - Edit HOME variable

The Getting Started instructions tell you to Create a GRAILS_HOME environment variable that points to the path where you extracted the archive. On the Apple this is in a file called profile which is found in the etc folder.

In the Textmate ‘open file’ dialog box (tick the Show Hidden Files checkbox) and edit /etc/profile to include the following:

export JAVA_HOME=/Library/Java/Home
export GROOVY_HOME=/Library/groovy-1.5.7
export GRAILS_HOME=/Library/grails-1.0.3
PATH=$PATH:$GROOVY_HOME/bin
PATH=$PATH:$GRAILS_HOME/bin

Step 4 - Check it out

By opening the terminal window (Applications/Utilities/Terminal) and typing groovysh you can check that Groovy was installed properly.

Then type grails to check that grails is installed.

Ruby on Rails vs Groovy and Grails

Ruby on Rails

Rails LogoRuby on Rails is now on version 3 and has a big following. In my opinion it is just trying to do too much.

Groovy and Grails

CSS Edit LogoI really like Groovy and Grails, but the cost of hosting excludes it for my smaller clients.

Horses for Courses

The choice of language and framework may well be dictated by your client or employer, but if you are an independent developer it is worth finding the framework that makes you most productive.

I have chosen another path - a path less trodden ...

Other PHP Frameworks

The heavy weights

I hadn't realised how many PHP frameworks there are out there. I spent a couple of weeks looking for a framework that suited me.

The most popular are Zend, CodeIgniter, Symfony, Yii, Kohana and Cakephp. My problem with these is that the are too heavyweight and with too steep a learning curve.

The light weights

So I started downloading and testing some of the lightweight frameworks such as Agile, Agile Tool Kit, FatFree, Faster, Hydrogen, LiteVC, MicroMVC, Shine, Simple, TinyMVC ...

The list goes on, and on. But all the frameworks I tried came with their own idiosyncrasies. So tried to sum up what it was I was looking for:

Another few google searches and I found phpAgile ...

phpAgile - the minimalist PHP framework

Virgin Territory

When I found phpAgile at the google code repository I think I was the first person to download the sample. Not a good sign, but this was about the first sample that worked, pretty much out of the box, and somehow ... it just felt right.

I think this is mainly down to the way it has been based on Grails. Nicolas Capeda, the author, is a Grails developer and has put together the most minimalist PHP framework to provide the routing and a simple ORM system.

Database Access

Unfortunately, the database handling didn't match my configuration, and I had already decided that PDO was the best way to go with PHP and MySQL (and most other) databases.

So I have re-written the database classes using PDO, and moved away from the ORM approach. As a long-in-the-tooth database developer I would rather specify how my objects map to the database. A simple set of PDO-based classes now manage database access and these are called from the Model layer where any specific application SQL is located.

And it just works. Simples!

Sample Application

I have now developed a sample application, which I will soon send to Nicolas Capeda, to see if he approves of my approach. If all goes well I hope that my changes to phpAgile will be the first formal release.

There will be more on this in forthcoming blogs.

Sproutcore

Netbeans and Sproutcore

I intend to use MySQL as my database of choice for web applications. I have been using Textmate to develop Groovy / Grails applications, but Netbeans V6.5 (RC) now supports Groovy and Grails. I can't quite bring myself to move away from the simplicity of Textmate but will check out Netbeans 6.5 as it matures.

Installing Netbeans and MySQL

Go to the www.netbeans.org website for the latest downloads. I installed the Netbeans + MySQL (Version 6.1) bundle first - this is a quick and simple way to get going with MySQL (Don't forget to install the MySQL preference pane). Then I downloaded the Version 6.5 (Release candidate). Just follow the instructions; if only everything was this simple.

Note: You will need to set Groovy Home in the Netbeans preferences (Miscellaneous/Groovy) to /Library/grails-1.0.3

For a good introduction to Groovy and Grails take a look at Jason Ruldolph’s (free) PDF Getting Started with Groovy and Grails.

Other essential reading is Beginning Groovy and Grails by Christopher Judd, Joseph Nusairat and James Shingler and The Definitive Guide to Grails (Version 2) by Greame Rocher and Jeff Brown

Does it work?

This looks interesting. Sproutcore is a framework for developing desk-top like applications for browsers. It has been used by Apple on their Mobile.Me application. Take a look at www.sproutcore.com.

I will report back when I have used it a bit more, but in the short term I am not sure if a have a real use for it.

<<  back to Blogs page