Using Lithium in Drupal 7
Let me start off by saying that the Lithium framework is awesome. It allows the programmer to write very elegant code, something that cannot be said about many of the other PHP frameworks out there. Also, it's very loosely-coupled, and many of it's components can be used separately and integrated with other applications.
Lithium prides iteself upon being easy to integrate into other systems. Let's put that to the test in Drupal 7. I am planning on a "full blown" Lithium module for Drupal that will allow for other modules to easily hook into the bootstrap process. The code below is meant to show how easy it is to use Lithium within a Drupal 7 application.
First, download the lithium core library and place it in /sites/all/libraries.
Then, create a custom module and include the following lines at the top of it:
define('LITHIUM_LIBRARY_PATH', './sites/all/libraries');
require_once(LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php');Next, implement hook_init() and bootstrap the Lithium core library and your custom module library:
function mymodule_init() {
\lithium\core\Libraries::add('lithium');
\lithium\core\Libraries::add('mymodule', array('path' => __DIR__));
// Now, you can add a database connection or any other bootstrap code.
}That's it! You can now use classes from the Lithium core library and your own custom library in your custom modules and themes. Here's a simple example:
function mymodule_node_view($node, $view_mode, $langcode) {
echo \lithium\util\String::insert('Now viewing node {:nid}', array('nid' => $node->nid));
// If you setup a connection and created a model at /sites/all/modules/mymodule/models/Foo.php, you can do this:
$foo = \mymodule\models\Foo::findById(1);
}