Blog: Yet Another Propel Behavior: Sluggable

The Propel Team – 19 January 2010

The development on the Propel 1.5 branch keeps up at a good pace. Today, a new addition was made to the list of the available Propel behaviors: sluggable

It does exactly what you would expect: automatically compose a unique slug for every object that you save. The slug can be used to provide friendly URLs:
$post1 = new Post();
$post1->setTitle('How Is Life On Earth?');
$post1->setContent('Lorem Ipsum...');
$post1->save();
echo $post1->getSlug(); // '/posts/how-is-life-on-earth' 
Once your objects have slugs, it is very easy to find the object matching a given slug - for instance, from an URL:
$post = PostQuery::create()->findOneBySlug('/posts/how-is-life-on-earth');
As for other behaviors, it it dead simple to initialize. Just add the sluggable behavior tag in your schema, rebuild your model, and you're ready to go:
<table name="post">
  <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  <column name="title" type="VARCHAR" required="true" primaryString="true" /
  <column name="content" type="LONGVARCHAR"/>
  <behavior name="sluggable">
    <parameter name="slug_pattern" value="/posts/{Title}" />
  </behavior>
</table>
Make sure you read the sluggable documentation to see all the available settings to customize this brand new behavior.