Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created May 27, 2011 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromi2424/995223 to your computer and use it in GitHub Desktop.
Save hiromi2424/995223 to your computer and use it in GitHub Desktop.
MultiSlugRoute design
<?php
class Report extends AppModel {
// set true when type => daily was affected
private $__dailyReportAffected = false;
/*
some implemetations...
*/
function _clearCache($type = null) {
if ($this->__dailyReportAffected) {
MultiSlugRoute::clearCache('DailyReport');
$this->__dailyReportAffected = false;
}
parent::__clearCache($type);
}
}
<?php
Router::connect(
'/report/daily/:username/:title/:written_date',
array(
'controller' => 'reports',
'action' => 'daily',
),
array(
'routeClass' => 'MultiSlugRoute',
'slugs' = array(
// uses username for slug and display
'username' => array(
'model' => 'User',
),
'title',
'written_date' => array(
// manually specify
'model' => 'DailyReport', // common model(Report) by default
'display' => 'written_date', // field name to show, slug(key of slugs array) by default
'urlencode' => false,
// give functions when you want to change slug from field value
'callbacks' => array(
// converting
'parse' => function($slug) {
return str_replace('-', '.', $param);
},
// reversing
'match' => function($param) {
return str_replace('.', '-', $param);
},
),
),
),
/* options */
'model' => 'Report', // default, inflected from controller name
'cacheConfig' => 'slugs', // 'default' by default
'identity' => 'DailyReport', // :alias(Model::$alias) by default, useful for cache control
'urlencode' => true, // default, perhaps required whenever
'conditions' => array(
'type' => 'daily',
), // empty array by default
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment