Back to frontpage

Three things I love about Lithium

Lithium, the most RAD PHP framework, is slowly grinding towards a 1.0 release, so I figured I wanted to highlight three things I really like about the framework.

1. Clutter free

I believe a framework — or any tool — should assist you without getting in your way, and I think many frameworks fail at this step. I want a framework to help me organise my code and write less code, and if you look at the following Lithium controller code you can see there is hardly any crud code at all:

<?php
use app\models\Model;
class FooController extends lithium\action\Controller {
    /**
     * Will render the views/foo/bar.html.php view by default
     */
    public function bar() {
        return array(
            'results' => Model::all()
        );
    }
}

Whats awesome is that you can now reach /foo/bar.html and /foo/bar.json and it just works. This magic happens because the response assembling by default happens later. You can add support for other file types as well which can be negated by the URL file ending or headers. For example

2. Filters

The most powerful feature of Lithium is its Aspect Oriented Programming capabilities for solving cross-cutting concerns, as AOP is intended for. Lots of Lithium core methods are filterable, and you can write your own methods so that they become filterable. Look at this filter for Model::save:

<?php
Model::applyFilter('save', function($self, $params, $chain) {
    $params['entity']->modified = time();
    $result = $chain->next($self, $params, $chain);
    // This will happen after a succesfull save
    if ($result) {
        ExternalSync::syncObject($params['entity']);
    }
    return $result;
});

Its easy to see how this can be used to solve a number of things quite easily; You could apply an application wide caching without touching zilch existing code, and you can reuse that code across applications! You could add default fields (created, modified) once and for all, without a base class.

3. Consistent

This is a damn solid proof of damn solid design; Most methods in Lithium has a consistent signature, making your fluency drastically better as you get better:

<?php
Model::find($type, array $options);
Model::save($entity, $data, array $options);
Collection::map($callback, array $options);
Media::type($type, $content, array $options);
Router::connect($template, $params, array $options);
Controller::redirect($url, array $options);
Auth::set($name, $data, array $options);

As you can see argument numbers are kept really low, almost all methods in the codebase has 1-3 arguments, and the last argument is almost always an options array. Some dont like option arrays due to IDE autocompletion, but personally I absolutely love the method signatures.

So, do you agree that this looks pretty rad? Go check out some more: