Skip to content

Instantly share code, notes, and snippets.

@arghav
Created November 21, 2010 09:56
Show Gist options
  • Select an option

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

Select an option

Save arghav/708610 to your computer and use it in GitHub Desktop.
Lithium Filter 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;
});
@rudiedirkx

Copy link
Copy Markdown

Where would this filter be located? In what file? How would it be loaded?

@mariuskubilius

Copy link
Copy Markdown

$params['data']['title']; does not return title anymore. you should go with $params['entity']->title;

@arghav

arghav commented Oct 6, 2011

Copy link
Copy Markdown
Author

This was written when I was trying out Lithium, I'm not using Lithium anymore. You can probably fork and update this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment