(c) 2006-2007 Guillermo Rauch (http://devthought.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Installation for sf 1.2
php symfony plugin:install sfPropelActAsSluggableBehaviorPlugin --release=0.2.0
Installation for sf 1.1
php symfony plugin:install sfPropelActAsSluggableBehaviorPlugin --release=0.2.0
Installation for sf 1.0
php symfony plugin-install http://plugins.symfony-project.org/sfPropelActAsSluggableBehaviorPlugin
PEAR
Download the PEAR package
Repository
Go to the repository: http://svn.symfony-project.com/plugins/sfPropelActAsSluggableBehaviorPlugin
sfPropelActAsSluggableBehaviorPlugin plugin
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.
For example, for a blog post whose title is 'A post', this behavior will fill the slug database
field with 'a_post'. If a_post exists, a_post_1 is used, and so on.
Instalation
Install the plugin
symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsSluggableBehaviorPlugin
Add the slug field to your schema.yml for a desired table
...
title: varchar(255)
slug: varchar(255)
...
Enable Propel behavior support in propel.ini:
propel.builder.AddBehaviors = true
If you have to enable the behavior support, rebuild your model:
symfony propel-build-model
Enable the behavior for one of your Propel model:
<?php
// lib/model/ForumPost.php
class ForumPost
{
}
$columns_map = array('from' => ForumPostPeer::TITLE,
'to' => ForumPostPeer::SLUG);
sfPropelBehavior::add('ForumPost', array('sfPropelActAsSluggableBehavior' => array('columns' => $columns_map, 'separator' => '_', 'permanent' => true)));
The column map is used by the behavior to know which columns hold information it needs :
- from : Model column holding the string to convert. For example, the post title. ('title' by default)
- to : Model column holding the generated string, aka slug. ('slug' by default)
The separator parameter is used by the behavior to replace the whitespaces. ('-' by default)
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)
Usage
Every time a post is saved, the slug will be generated automatically. You can later access it
through ->getSlug() for example.