Das123
Das123

Reputation: 865

Kohana 3.1 routes with default subdirectories

I have an application that is essentially working until I tried to implement an Auth module for logins and registrations.

My application directory structure is basically:

application
-- classes
  -- controller
     -- admin
        (admin area)
     -- block
        (blocks to display within pages)
     -- page
        (default pages)

By default I want to have URL's such as http://www.testsite.com/test which would access the Controller_Page_Test class. Or to explicitly call admin or block pages http://www.testsite.com/admin/test which would access the Controller_Admin_Test class. To further complicate matters it also needs to handle optional actions and id's.

I said at the top that this is basically working correctly - until I've tried to add in the Auth module. The Auth module calls http://www.testsite.com/user/login but instead of accessing the module's path via the default, it looks in the page directory.

To overcome this I placed a higher level route but now this has become my default page handler. Explicit calls still get through.

My routes currently look like this:

Route::set('user', '(<controller>(/<action>(/<id>)))', array('controller' => 'user|admin_user'))
    ->defaults(array(
        'controller' => 'user',
        'action'     => 'index',
        'id'         => NULL,
    ));

Route::set('with_dir', '<directory>/<controller>(/<action>(/<id>))', array('directory' => 'block|admin'))
    ->defaults(array(
        'directory'  => 'page',
        'controller' => 'home',
        'action'     => 'index',
    ));

Route::set('just_id', '<controller>(/<id>)', array('id' => '\d+'))
    ->defaults(array(
        'directory'  => 'page',
        'controller' => 'home',
        'action'     => 'index',
    ));

Route::set('auto_dir', '<controller>(/<action>(/<id>))', array('id' => '\d+'))
    ->defaults(array(
        'directory'  => 'page',
        'controller' => 'home',
        'action'     => 'index',
    ));


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

Can this be cleaned up any better? And how do I get this module to only kick in when I need it to?

Upvotes: 0

Views: 534

Answers (1)

Tadeck
Tadeck

Reputation: 137440

Yes, it can be cleaned better. Kohana developers encourage people using this framework to add as much routes as needed. You can even specify them for each action which will enable you to change URLs in the future (eg. instead of /user/login you may wish to have /signin), if you use proper methods to generate links etc. (eg. Route::url() helper).

Now, saying that, here is the other way to specify the user route:

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

Which will match only the requests, where the first part of the URI is given and is equal either to user or admin_user. Previously the controller part was optional, thus was also matching calls to / URI.

Upvotes: 1

Related Questions