So-called microframeworks such as Sinatra and Camping were created to provide developers with a lightweight alternative to Rails. If your requirements are relatively straightforward, these lightweight solutions can dramatically reduce the learning curve and time required to complete the project. The PHP Limonade framework was created with this goal in mind, providing PHP developers with a low barrier-of-entry to the world of framework driven development without incurring lengthy training overhead.
In this article I'll introduce you to Limonade, highlighting its key features by building a simple exercise diary (incidentally, this Limonade introduction is the latest installment of my ongoing review of lightweight PHP frameworks see Fat-Free, DooPHP, and my top ten list of lightweight frameworks).

Making Limonade

To install Limonade, download the latest stable version from its GitHub repository. Unzip the contents and move the lib directory into your Web project's home directory. Next, create a file named index.php and add the following contents to it:
<?php
require_once 'lib/limonade.php';
// Application code goes here
run();
Next, create an .htaccess file which will route all requests to a file named index.php:
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  Options +Indexes
  RewriteEngine on
  # RewriteBase /path/ # If website in subdirectory, uncomment and change this
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^(.*)$   index.php?/$1    [NC,L]
</IfModule>
Believe it or not you're ready to start using Limonade!

Creating Your First Route

Limonade users create a Web site by defining a set of custom routes, mapping each route to an associated method. Custom routes are defined using Limonade's dispatch() method, which you'll use to identify each end point and its associated method. For instance, the following dispatch() call ties a method named welcome() to the website's home page:
dispatch('/', 'welcome');
With the route defined, you'll next want to define the welcome() method and within it, use Limonade's html() method to identify a view (page template) associated with the page:
function welcome()
{
  return html('index.html.php');
}
Limonade will by default look for a directory named views within your project home directory, so create that directory and within it, create a file named index.html.php, adding the following contents:
<html>
  <head>
    <title>Welcome to My Exercise Diary</title>
  </head>
  <body>
    <p>
    Return often to receive updates about the trials and tribulations
    surrounding raising my heart rate beyond 80bpm!
    </p>
  </body>
</html>
Now open up your browser and head over to the website's home page, and you'll see the contents of the view file rendered to the window, as shown in Figure 1.


Limonade PHP Framework
Click here for larger image

Figure 1. A Limonade-powered Homepage

From here you can begin building out the rest of your website's end points, adding a dispatch() call and associated method for each. For instance, a route definition pointing to /about would look like this:
dispatch('/about', 'about');
function about()
{
  return html('about.html.php');
}

Using a Layout

While it's possible to use Limonade to create a series of static pages, doing so would defeat the purpose of embracing a framework. Limonade offers a great many ways to reduce the amount of redundant code you'll need to write, starting notably with the website layout template. If you were to create multiple pages using the same approach as that demonstrated for creating the home page, then updating something as simple as the website's title (nestled between the title tags) would be quite a chore. Fortunately, you can eliminate the redundancy by using Limonade's layout feature. Let's explore this feature by modifying your welcome() method call to look like this:
function welcome()
{
  return html('index.html.php', 'layout.html.php');
}
The layout.html.php file contains the global template which the associated view is injected into with each request. The injection occurs where the global template outputs the $content variable, as demonstrated here:
<html>
  <head>
    <title>Welcome to My Exercise Diary</title>
  </head>
  <body>
    <?php echo $content; ?>
  </body>
</html>
Next, strip the template-related tags from the index.html.php view, leaving only the introductory paragraph:
<p>
Return often to receive updates about the trials and tribulations
surrounding raising my heart rate beyond 80bpm!
</p>
Finally reload the page and you'll see that the view has been injected into the layout!

Listing Blog Posts

Although Limonade is perfectly capable of talking to a database, I'd rather manage the entries in a series of text files, and use Limonade to retrieve and parse these files. Each file is named in accordance with the current date, such as 2011-04-12.txt. The following posts route will retrieve all of the file names found in a directory named entries, sorting the file names in reverse chronological order, and create user-friendly versions of those dates:
dispatch('/posts', 'posts');
function posts()
{
  // Retrieve all of the entries from the entries directory
  foreach (glob("entries/*.txt") as $filename) {
      $filenames[] = basename($filename, '.txt');
  }
  // Sort the files backwards according to date
  krsort($filenames);
  // Iterate over each file name and 
  // create a user-friendly version of the date
  foreach ($filenames AS $filename)
  {
    $date = new DateTime($filename);
    $dates[] = array('name' => $filename, 
                     'date' => $date->format('F j, Y'));
  }
  // Set the $dates variable to the view scope
  set('dates', $dates);
  return html('posts.html.php', 'layout.html.php');
}
Loading the /posts route produces output similar to that shown in Figure 2.

Limonade PHP Framework
Figure 2. Displaying Blog Entries with Limonade Framework

Displaying a Blog Entry

Each link has been formatted to look like this:
http://dev.example.com/view/2011-04-13
Therefore the only remaining task is to create the view route, passing the date into the route so we can retrieve the entry:
dispatch('/view/:date', 'view');
function view()
{
  $date = params('date');
  $entry = file_get_contents("entries/".$date.".txt");
  set('entry', nl2br($entry));
  return html('entry.html.php', 'layout.html.php');
}
Now click on one of the links and the entry will be displayed within the browser window!

Conclusion

The Limonade microframework makes building dynamic websites dead simple, capable of accomplishing quite a bit with a few scant lines of code. Are you doing anything interesting with Limonade? Tell us about it in the comments!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.