简体   繁体   中英

kohana 3.0 directory to controller

I have my website under Kohana 3.0 which works perfectly with the defaut Route

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

When I try to access my website at this address http://127.0.0.1/web/ It loads the url http://127.0.0.1/web/user . It is OK. But now I want to add the admin directory under the controller. So my web tree looks like this

classes
|  controller/
       Admin/
          dashboard
       web.php
|  model

I would like to allow the Admin to access the admin's page in a url like this http://127.0.0.1/admin/dashboard . Where dashboard is the controller under the admin's directory. I modify the bootstrap file with this

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

I can access the admin session through http://127.0.0.1/web/admin/dashboard/ But I can't access the default controller that is http://127.0.0.1/web/ . The error Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: I am missing the default access for the controller. How can I set Route it to make access to my web site either through the link:

http://127.0.0.1/web/

and

http://127.0.0.1/web/admin/dashboard/

EDIT From the kohana documentation, it is written

In this example, we have controllers in two directories, admin and affiliate. Because this route will only match urls that begin with admin or affiliate, the default route would still work for controllers in classes/controller.
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
    array(
        'directory' => '(admin|affiliate)'
    ))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

Source : http://kohanaframework.org/3.0/guide/kohana/routing#examples

Now I modify my code to

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

but I have this error

Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: 

when I want to access the default controller like http://127.0.0.1/user/index

This route would translate to: http://127.0.0.1/admin/web , but your Admin folder would need to have user controller inside.

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

If you want the directory to be optional, you'd need to

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

But, in your case, you need multiple routes. Above the "catch all" route, put this:

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

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

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

Try to insert the directory inside ->defaults

Route::set('whatever', 'whatever')
    ->defaults(array(
    'directory' => 'admin',
    'controller' => 'user',
    'action' => 'index',
    ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM