| 1 |
= sfPropelActAsSluggableBehaviorPlugin plugin = |
|---|
| 2 |
|
|---|
| 3 |
This `sfPropelActAsSluggableBehaviorPlugin` plugin that automates the generation of 'slugs' based on the return value of a model method. These are useful to hide the primary key in routing requests, and make your urls look fancy. |
|---|
| 4 |
|
|---|
| 5 |
For example, for a blog post whose title is 'A post', this behavior will fill the slug database |
|---|
| 6 |
field with 'a_post'. If a_post exists, a_post_1 is used, and so on. |
|---|
| 7 |
|
|---|
| 8 |
== Instalation == |
|---|
| 9 |
|
|---|
| 10 |
* Install the plugin |
|---|
| 11 |
|
|---|
| 12 |
{{{ |
|---|
| 13 |
symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsSluggableBehaviorPlugin |
|---|
| 14 |
}}} |
|---|
| 15 |
|
|---|
| 16 |
* Add the slug field to your schema.yml for a desired table |
|---|
| 17 |
|
|---|
| 18 |
{{{ |
|---|
| 19 |
... |
|---|
| 20 |
title: varchar(255) |
|---|
| 21 |
slug: varchar(255) |
|---|
| 22 |
... |
|---|
| 23 |
}}} |
|---|
| 24 |
|
|---|
| 25 |
* Enable Propel behavior support in `propel.ini`: |
|---|
| 26 |
|
|---|
| 27 |
{{{ |
|---|
| 28 |
propel.builder.AddBehaviors = true |
|---|
| 29 |
}}} |
|---|
| 30 |
|
|---|
| 31 |
If you have to enable the behavior support, rebuild your model: |
|---|
| 32 |
|
|---|
| 33 |
{{{ |
|---|
| 34 |
symfony propel-build-model |
|---|
| 35 |
}}} |
|---|
| 36 |
|
|---|
| 37 |
* Enable the behavior for one of your Propel model: |
|---|
| 38 |
|
|---|
| 39 |
{{{ |
|---|
| 40 |
#!php |
|---|
| 41 |
<?php |
|---|
| 42 |
// lib/model/ForumPost.php |
|---|
| 43 |
class ForumPost |
|---|
| 44 |
{ |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
$columns_map = array('from' => ForumPostPeer::TITLE, |
|---|
| 48 |
'to' => ForumPostPeer::SLUG); |
|---|
| 49 |
|
|---|
| 50 |
sfPropelBehavior::add('ForumPost', array('sfPropelActAsSluggableBehavior' => array('columns' => $columns_map, 'separator' => '_', 'permanent' => true))); |
|---|
| 51 |
}}} |
|---|
| 52 |
|
|---|
| 53 |
The ''column map'' is used by the behavior to know which columns hold information it needs : |
|---|
| 54 |
|
|---|
| 55 |
* from : Model column holding the string to convert. For example, the post title. ('title' by default) |
|---|
| 56 |
* to : Model column holding the generated string, aka slug. ('slug' by default) |
|---|
| 57 |
|
|---|
| 58 |
The ''separator'' parameter is used by the behavior to replace the whitespaces. ('-' by default) |
|---|
| 59 |
The ''permanent'' paramter is used by the behavior to avoid generating the slug when the model is updating the ''from'' column, useful for permalinks. (false by default) |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
== Usage == |
|---|
| 63 |
|
|---|
| 64 |
Every time a post is saved, the slug will be generated automatically. You can later access it |
|---|
| 65 |
through ->getSlug() for example. |
|---|