Skip to content

Instantly share code, notes, and snippets.

@arghav
Created November 21, 2010 08:41
Show Gist options
  • Select an option

  • Save arghav/708577 to your computer and use it in GitHub Desktop.

Select an option

Save arghav/708577 to your computer and use it in GitHub Desktop.
Lithium Filters Practical Example
<?php
namespace app\controllers;
use \app\models\Post;
use \lithium\util\Inflector;
Post::applyFilter('save', function($self, $params, $chain) {
// Custom pre-dispatch logic goes here
//create a slug for new posts.
if(!($params['entity']->exists())){
$slug = Inflector::slug($params['data']['title']);
//count of slugs which start with $slug
$count = Post::find('count', array(
'fields' => array('id'),
'conditions' => array('slug' => array('like' => '/^(?:' . $slug . ')(?:-?)(?:\d?)$/i')),
));
$params['data']['slug'] = $slug . ($count ? "-" . (++$count) : ''); //add slug-X only if $count > 0
}
$response = $chain->next($self, $params, $chain);
// $response now contains the return value of the dispatched request,
// and can be modified as appropriate
// ...
return $response;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment