Search code examples
phprouteskohana

Kohana: Omit action from url


Is there any way I can default a route to use action_index and not have to specify it in the url?

ie.

Route::set('user_profile','(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory' => 'public',
        'controller' => 'user',
        'action'     => 'index',
    ));

To use that I need to specify /users/index/1234

But I'd like to use /users/1234

I tried taking out action from the Route::set() but I ended up with a 404 page.

UPDATE

Now that I have added this route (the top one) my default route doesn't seem to be working now

Route::set('user_profile','(<controller>(/<id>))')
->defaults(array(
    'directory' => 'public',
    'controller' => 'users',  // Note I changed it to plural to match 'users/*' from your url
    'action'     => 'index',
));

Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory' => 'public',
        'controller' => 'home',
        'action'     => 'index',
    ));

Solution

  • It's as simple as omitting the <action> param from the URL, but still keeping the default value:

    Route::set('user_profile','(<controller>(/<id>))')
        ->defaults(array(
            'directory' => 'public',
            'controller' => 'users',  // Note I changed it to plural to match 'users/*' from your url
            'action'     => 'index',
        ));
    

    Note, that unless you have other Route that overrides this behaviour, your user controller will only be able to execute the index action.


    Edit

    If your users_profile route is only handling /users path then you can set it in the route explicitly:

    Route::set('user_profile','users(/<id>)')
        ->defaults(array(
            'directory' => 'public',
            'controller' => 'users',  // Note I changed it to plural to match 'users/*' from your url
            'action'     => 'index',
        ));
    

    This should address the conflicting routes.