add flavor to your CakePHP applications with some tasty syrup

Sluggable Behavior

This behavior will let you create unique slugs for every record added (or updated) on any of the models you attach it to. This is particularly useful to create SEO links out of, for example, a table of articles. Instead of seeing those ugly /articles/view/4 URLs, use the Sluggable Behavior and easily accept URLs such as /articles/view/my-seo-friendly-article. It handles the slug creation, slug collision, and allows you to specify different settings such as the separator to use, maximum width of a slug, among other useful parameters.

Quick Introduction

Say we have an Article which has a field named ‘title’ and another one named ’slug’, among others. We want to automatically generate slugs in the field ’slug’ whenever a new article is created, using the string provided in the field ‘title’. For example, if a new article is created with the title ‘My Example Article’, the field ’slug’ should be populated with the value ‘my-example-article’. This will let us build links using the slug instead of the ID, and then doing an easy $this->Article->findBySlug(’my-example-article’) from our controllers. All we have to do to get this, and more, is to add the Sluggable behavior to our Article model’s actsAs parameter:

class Article extends AppModel {
	var $name = 'Article';
	var $actsAs = array('Sluggable');
}

It’s that easy! And best of all we can use different settings to let it know slugs should also be re-generated on updates, to build the slug using a concatenation of fields, use a different separator, or more. Go ahead and read the tutorial at the bakery to learn all there is to learn.

Download

To download Sluggable Behavior and other Cake Syrup ingredients go to the download page.