front and back-end web development, Leeds, UK

Richard's Blog - Design, coding and life in Japan


Weaving Lithium #li3 into a legacy PHP application incrementally

Legacy Application

I have a legacy application which was originally written in straight PHP with a few classes that I have created for convenience, but this is old and I am wanting to get this into a modern web framework. I have prepared it by stripping out all of the old Sql and made all Database requests through the excellent Doctrine 2. I had also heard that Lithium can support Doctrine 2 through the li3_doctrine plugin found here.

If you are doing the same I also recommend that you separate your HTML code from your application logic like I had done anyway. I had layout templates, html modules and headers which I called before any HTML was parsed.

Why Lithium?

Lithium is heavily decoupled, I like this in a framework. This means that I can now start adding parts of it that I want to incrementally. I will eventually port this over Lithiums full stack when I am using more of its pieces. For now I will focus on just importing the bits I need.

I wanted to update my site with better validations anyway so I will start with flash messages and then validations. For flash messages in lithium you will need the li3_flash_message plugin

The Bootstrap

Lithium loads up most of the framework, libraries and settings using pretty simple bootstrap files. After looking at them I realized that a lot was being loaded up that I didn't yet need, and I certainly didn't want the dispatcher and router from stopping parts of my application from working. I will attack those later. I imported the following file to my app


define('LITHIUM_LIBRARY_PATH', dirname(__DIR__) . '/libraries');
define('LITHIUM_APP_PATH', dirname(__DIR__).'/app');

if (!include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php') {
	$message  = "Lithium core could not be found.  Check the value of LITHIUM_LIBRARY_PATH in ";
	$message .= "config/bootstrap.php.  It should point to the directory containing your ";
	$message .= "/libraries directory.";
	trigger_error($message, E_USER_ERROR);

use lithium\core\Libraries;

Be careful to add the correct paths to the main lithium library, I created the main app path to a fresh lithium app which I will be using more and more to when I get closer to using the full stack.

Now I have lithium loaded! and no errors.


I added the following to my bootstrap:


use \lithium\storage\Session;
    'flash_message' => array('adapter' => 'Php')

I can now use flash messages in my app and for now I will add this to my legacy flash message functions which I am using in my app.

use li3_flash_message\extensions\storage\FlashMessage;

function set_flash_message($message){

function display_flash_message(){
    $message = FlashMessage::get();
        return '<div class="flash-message">'.$message['message'].'</div>';
    return null;

I am now able to use li3_flash_message in my legacy app.

Using Validations

I am now even able to use my validations like below:

use \lithium\util\Validator;

  $rules = array(
    'Name' => 'Please enter your name',
		'Email' => array(
			array('notEmpty', 'message' => 'Email is empty'),
			array('email', 'message' => 'Email is not valid'),
	$result = Validator::check($_POST, $rules);
	if(preg_match('/www.|http:|https:|\[url]|script|url=|href/', $_POST['Questions_Comments'])){
	  $result[] = array('Questions_Comments'=>'You are not able to send urls through this form');
	  $result[] = array('Email'=>'Email and confirmation do not match');


I will also quickly update the flash message to help me work with the Lithium validation results:

function set_flash_message($message){
    $display = '<ul>';
    foreach($message as $key=>$val){
      foreach($val as $entry){
  else {
    $display = $message;


Recent Blog Posts