tags:

views:

484

answers:

2

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';
A: 

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.

jitter
+2  A: 

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');
D. Evans
This looks promising! I already have a 404 hook and default controller hook, so implementing this should be a breeze.
alex
Worked great - you're the man!
alex