Created
November 21, 2010 08:41
-
-
Save arghav/708577 to your computer and use it in GitHub Desktop.
Lithium Filters Practical 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; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment