Amal Raghav

Design. Develop. Destroy.

Lithium Filters - A Practical Example

If you skimmed through the features of Lithium framework, you might have seen something called Filters. Here I'm just giving the sample code I used in one of my pet project with Lithium, where I used Filters.

The idea was to create a slug for the blog post. The model is Post and we use the Inflector library for creating the slug. So lets have a look at the code.

As you can see, the Inflector class is used to create the slug. This class has many reusable string functions. The filter is applied to the save() of the Post model. So whenever the save() method is called in the controller, the filter will be applied. In the latest version of Lithium, we have a new feature to lazy load the filters. So lets see how we can use lazy-load here.

If you see, we now used the namespace of the model. This way Filter will be applied only when the Post class is loaded. You can find a little more about lazy-loading here.

Copyright © 2013 Amal Raghav