Created
November 21, 2010 09:56
-
-
Save arghav/708610 to your computer and use it in GitHub Desktop.
Lithium Filter Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| }); |
$params['data']['title']; does not return title anymore. you should go with $params['entity']->title;
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
Where would this filter be located? In what file? How would it be loaded?