Skip to content

Instantly share code, notes, and snippets.

@tigrang
Created August 3, 2012 00:10
Show Gist options
  • Save tigrang/3242448 to your computer and use it in GitHub Desktop.
Save tigrang/3242448 to your computer and use it in GitHub Desktop.
Hyphenated actions in urls
<?php
App::uses('CakeRoute', 'Routing/Route');
class HyphenRoute extends CakeRoute {
public function parse($url) {
if ($route = parent::parse($url)) {
// don't allow camel-case actions in url to prevent duplicate urls pointed to same page
if (strtolower($route['action'] !== $route['action'])) {
throw new NotFoundException();
}
// convert hyphenated action back to camel-case
if (strpos($route['action'], '-') !== false) {
$route['action'] = Inflector::camelize(str_replace('-', ' ', $route['action']));
}
}
return $route;
}
protected function _writeUrl($params) {
$params['action'] = str_replace('_', '-', Inflector::underscore($params['action']));
return parent::_writeUrl($params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment