Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email:


The longer Lithium Blog tutorial using MySQL – Part 1

Lithium is a lightweight, fast, flexible framework for PHP 5.3+. It is still in dev release state but is under active development. This post, as the title suggests, is to introduce you to this new framework using the typical blog tutorial.

Getting started
The first thing is to get the Lithium source code. You can either download the latest release from their download page or clone the git repository using –

In both the cases, place the framework your webroot like /var/www/html or any equivalent location on your setup. Access the URL in browser and follow the instructions to setup the framework before we can actually begin with blog tutorial. When everything is setup correctly you should see a page like

Create database
Let’s first create a database. I have named my database as li_blog. Feel free to use the name of your choice. Now create the table required for our tutorial and populate it with some default data using the following SQL


Open your app/config/route.php and replace the last section with the following. This is required in order to allow the pagination for index page to work

Displaying list of posts

Now that we already have some default posts in our database, let’s begin with displaying the list of posts in index page. We will be showing 5 posts per page in descending order of creation date. For that lets begin with creating a model. Create a new file in your app/models and name it Post.php and add the following code to it

Notice that we have written a custom finder to count the number of posts. This function will get executed whenever we call find(‘count’) on Post.

Now lets create a new file in app/controllers and name it PostsController.php Add the following code to the file

Almost all code here is self explanatory. Note that every action must return the variables to make them available in views. Also, by default, find() returns the object of model. We are calling a method to(‘array’) for convenience.

Now that we have all the data required to display the list of posts, lets create a view for displaying it. Create a new fie in app/views/posts (create the directory posts first) and name it index.html.php. Note that Lithium follows some naming conventions and all the filenames and class names must be according to those conventions only. Add the following code to the newly created view file

Lithium comes with default helpers for html and form rendering and are auto-loaded in rendering context. Just use them by referring as $this->html and $this->form.

Go to your browser and point it to http://localhost/lithium/posts and you should see a list of posts with pagination.

Adding new post

We will now proceed with adding a new post entry for our blog. In that process, we will also see how to add validations, use custom validations and use of save filter.

Open your PostControllers.php and add the following method to it after the existing index method.

The POST data is made available to us through $this->request->data. We are simply checking whether the post data is available and if so save it by calling the save() method. Before the data gets saved in the database, I would like my data to be validated. For this, I first need to specify the validation rules in the Post model. So now, open the model file Post.php and add the following just after the class definition

Here, notEmpty is the built-in validation rule and Lithium will check whether some title is entered or not. But isUniqueTitle is a custom validation rule as I want my post titles unique. Being a custom validation rule, I must write the implementation for this rule as well. Add the following code to __init() method just after count finder.

I also want to update the created/modified fields according to the action I am performing. Add the following code in continuation to the above code

Nothing very special in the above code except the return statement. We are calling next filter in the chain to make sure that all the filters run correctly. And now the form to add the data for post. Create a new file in app/views/posts and name it add.html.php. Add the following content to it

Form helper displays the validation errors for every field on its own. It creates a DIV element below the form element but doesn’t apply any CSS class to it. But Lithium provides us a way to override any of the default element templates. This is exactly what I have done on line 3 in above code. If any one of you know the better way please let me know, I will update my code accordingly.

Again, point your browser to http://localhost/lithium/posts/add. You should see a form to enter post. Play with it to test validations. Finally you should be able to save the data in database.

Editing the post

Next comes the edit post entry. Lets dive into the code directly. Add the following code to PostsController.php after add()

Simple, right? Just remember that Post::find() is must before we call save(). Otherwise it will always insert a new entry in database. The URL to access this page will be naturally http://localhost/lithium/posts/edit/1 where 1 is the id of the post to edit.

View is mostly similar to add page. Create a new file in app/views/posts and name it edit.html.php and add the following code to it

All the validation rules will be automatically applicable for edit as well. For our custom validation rule we have already taken care of edit functionality by checking for post id in data. So, no change required in model.

Lets now proceed to the final action of our post controller, delete. In PostsController.php add the following code after edit() action

That’s it. No view required for delete. The above code will check whether the post with given id really exists and if it does, deletes the post and redirects to index action.

The whole code used in this tutorial is available in Github repository. For the sake of better display, I have slightly modified the default layout and the style.css in webroot/css. You can check out those files from repository.

This is just the beginning. A lot more can be added to this like adding comments for posts. This will follow in the next part. Till then try out this tutorial and don’t forget to share your experience with us.

Read Part 2 here

This content may be a bit dated and may not work at all

About the Author


  1. Pingback: The longer Lithium Blog tutorial using MySQL – Part 1 at SANIsoft … « Source code bank

  2. Thanks for the post Aditya. Very useful. I think a thorough comparison between CakePHP and Lithium would be really helpful. Is that something you guys might be interested in compiling and blogging about?

  3. Pingback: SANIsoft Blog: The longer Lithium Blog tutorial using MySQL ‘” Part 1 | Development Blog With Code Updates :

  4. Pingback: SANIsoft Blog: The longer Lithium Blog tutorial using MySQL ‘” Part 1 |

  5. Pingback: Creating an AppModel in Lithium 0.9 » Patrick J Sparrow

  6. Hey man, can you go through your code by chance and comb for bugs, I’m seeing massive build-time errors in the PHP (like absence of starting html” ) which would really be difficult for most people to understand

  7. Pingback: List of useful Lithium (Li3) resources « Web mellom fjell

  8. Pingback: What are some good tips for using the Lithium Framework (for PHP)? - Quora

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.