Is there a generic way to route URLs in Kohana when there is the hyphen character in them?
Question
Kohana automatically sets up URLs like so
http://www.example.com/controller/method/argument1/argument2/etc
Now I like to use the dash to separate my words in the URL, and I have an address like so
http://www.example.com/business-hub
My controller is titled BusinessHub_Controller. What is annoying me, is for /business-hub/ to match the BusinesHub controller, I need to add a custom entry into the routes.php under the application/config folder. It also seems I have to add one for every method, which is really annoying. For example, here is an excerpt,
$config['business-hub'] = 'businesshub/index/';
$config['business-hub/logout'] = 'businesshub/logout';
$config['business-hub/media-releases'] = 'businesshub/mediareleases';
Obviously, this is really annoying. Is there anyway I can tell Kohana to convert the URL into the camelCase name, something like
$urlController = 'business-hub';
$correctController = str_replace('-', ' ', $urlController);
$correctController = ucwords($correctController);
$correctController = str_replace(' ', null, $correctController);
$correctController = $correctController . '_Controller';
Solution
Rather than just stripping out dashes, I'd convert them to underscores; and I'd do it using a hook. Make sure hooks are enabled in config/config.php and then create a file in hooks called, say, dashes_to_underscores.php:
function convert_dashes_to_underscores_in_url()
{
Router::$current_uri = str_replace('-', '_', Router::$current_uri);
}
Event::add_before(
'system.routing',
array('Router', 'setup'),
'convert_dashes_to_underscores_in_url');
OTHER TIPS
For the camelCase variant I don't know but something like this should work
$config['(a-z)+-?(a-z)*/(a-z)+-?(a-z)*'] = '$1$2/$3$4';
As the route part in kohana is a regular expression.
Of course this is severly limited to the cases provided by you.