Posts Tagged ‘micro framework’

Wing Commander

Thursday, July 5th, 2012 | Limited, Programming

Moustache I’m a big fan of the Flight PHP micro framework, and it’s built-in views component is quite nice when you want to do stuff in a rush.

But recently I have started to use Slim more often as it has almost out of the box support for Mustache templates, whereas Flight does not.

Until now, that is. I wanted to use Mustache with an existing project I had already coded up in Flight, so I’ve now written a wrapper for it and released it on Github. It’s nothing fancy – probably less than 100 lines of code in total. But it seems to get the job done.

So if you’re looking to use Mustache templates in your Flight application, why not give it a go? If you have any improvements, you can always fork it too!

Slim

Tuesday, July 3rd, 2012 | Limited, Programming

Slim is a PHP micro framework, similar to Flight, Silex and Limonade, not to be confused by the Ruby templating system of the same name. It claims to contain “everything you need and nothing you don’t.”

If functions like you would expect a micro framework to work. Here is the standard example.

<?php
require 'Slim/Slim.php';
$app = new Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name!";
});
$app->run();
?>

My favourite part of Slim, however, is actually the Slim Extras bundle which doesn’t ship with it, but can easily be downloaded and dropped in. This adds easy integration for lots of different templating languages – notably Smarty, Twig and Mustache.

So, for example, if you wanted to integrate Twig, just grab the Extras bundle, the Twig bundle, include the relevant file and change your initialisation statement to the following.

$app = new Slim(array("view" => "TwigView"));

That’s all – now you’re away and can call the render method with a filename and data array to render a template using Twig.

Limonade

Tuesday, June 19th, 2012 | Limited, Programming

I’ve recently written about Flight and Silex, so continuing the PHP micro framework theme, this post is looking at Limonade.

I had heard mixed reviews about it before I first began exploring the framework, and that pretty much sums up how I feel about it too. It does what it needs to, but there is nothing spectacular about it and given the choice, I would rather work with something like Flight.

It offers similar functionality, with an integrated views system, though without the object orientation you would expect, which makes it more similar to Sinatra. It also has a good range of hooks to allow you to extend functions by inserting code before and after.

Documentation for Limonade is a little hit and miss. There is quite a lot of it but it isn’t very well organised. If something isn’t in the main readme, you often have to go hunting around the examples folder until you find what you want. In its defence though, there is a lot of ground covered in the readme.

You can download it and have a play around from the Limonade website.

Creating a simple view helper in Flight

Monday, June 18th, 2012 | Limited, Programming

Recently I wrote about the Flight PHP micro-framework. One of the challenges I ran into when launching one of our sites in it was that we had some database information that needed to be pulled out in the layout.

It was the same information on every page so it didn’t make sense to pass it in as a variable as this would have meant using the same code on every route to do the same thing. Instead, it was simpler to create a view helper which would pull out the information needed.

class Helper {

	public static function menuTags () {
		$tagModel = Flight::tagModel();
		return $tagModel->findDistinct();
	}

}

Nothing fancy here, just a static method on a class which hooks into one of the model classes I has already registered with Flight and returns the result. I then just needed to reference this from the layout view.

<?php foreach (Helper::menuTags() as $tag) { ?>
<?=htmlspecialchars($tag["tag"])?>
<?php } ?>

Now it appears on every page without me having to pass it in as a variable.

Flight

Sunday, June 17th, 2012 | Limited, Programming

Flight is another micro framework written in PHP and bills itself as being extensively extensible.

Like other such frameworks, you begin (and arguably end) by creating a series of routes and adding closures to those routes that present the actions and outputs of said route. The quintessential example being as follows.

Flight::route('/hello', function(){
	echo 'Hello, World!';
});

The routing works really well using the simple format that allows you to name and route and add a regular expression match in one simple statement.

Flight::route('/article/@id:[0-9]+'

You don’t have to use closures either – any callable object can be used, so you can write a separate function and call it from multiple routes – useful if you have multiple, entirely separate patterns to match where it doesn’t make sense to do it all in one regular expression.

Flight has a simple built-in views system which allows you to render a view (which just uses PHP files to keep things simple) and pass an associative array of variables in which then translate to variables which you can use in the view. Layouts are also supported.

Its real power though is in how you can extend it. You can easily write your own classes and call them into your Flight methods using its register function.

Flight::register('db', 'Database');
$db = Flight::db();

This doesn’t just apply to new classes – you have override the Flight classes with your own if you wish to as well. Flight goes far deeper though, allowing you to do what it describes as filtering, a system which allows you to insert hooks before and after functions.

Flight is a great little framework, and one to rival Silex.

Silex

Saturday, June 9th, 2012 | Limited, Programming

Silex is a PHP microframework based on Symfony2 components.

With the shift in recent years to leveraging more JavaScript and front-end code in fat clients, a lot of server-side processing has been reduced to simple data relay and APIs. As a result, there have been a number of microframeworks arisen, which allow you to serve out content in a really simple and easy way.

One of the most popular is Sinatra, a micro-framework for Ruby, which is what we built Village Chief on. Indeed, Silex is inspired by Sinatra, but is PHP-based and uses some of the great components that can be found in the Symfony2 framework.

As you would expect from a microframework, it’s really easy to get started.

<?php
require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function ($name) use ($app) {
    return 'Hello '.$app->escape($name);
});

$app->run();

It relies heavily on Composer, a PHP dependency manager. This is a bit of a pain if you’re not already using Composer as it means you have to have yet another piece of software on your computer, but unfortunately, you’re somewhat railroaded into it as there is virtually no documentation on how to install things like Twig without it. Luckily, once you have it, it does make things easy and pain-free, so it’s probably worth going through the initial setup.

Once you’re up and running, it’s a snap to add content. We recently re-launched Maze Finance and the entire process of getting Silex up and running and migrating our existing website into it took less than two hours!