【问题标题】:kohana 3.0 directory to controllerkohana 3.0 目录到控制器
【发布时间】:2012-12-11 15:52:58
【问题描述】:

我的网站在 Kohana 3.0 下,与默认路由完美配合

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

当我尝试在此地址 http://127.0.0.1/web/ 访问我的网站时,它会加载网址 http://127.0.0.1/web/user 。没关系。 但是现在我想在控制器下添加管理目录。所以我的网络树看起来像这样

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

我想允许管理员以这样的 url 访问管理员页面 http://127.0.0.1/admin/dashboard。其中dashboard是管理员目录下的控制器。 我用这个修改引导文件

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

我可以通过http://127.0.0.1/web/admin/dashboard/ 访问管理会话 但我无法访问默认控制器 http://127.0.0.1/web/ 。错误Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: 我缺少控制器的默认访问权限。 如何设置 Route 它以通过链接访问我的网站:

http://127.0.0.1/web/

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

编辑 从kohana文档中,它是这样写的

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',
    ));

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

现在我将代码修改为

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

但我有这个错误

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

当我想访问像http://127.0.0.1/user/index这样的默认控制器时

【问题讨论】:

  • “web”文件夹不存在。它必须是http://127.0.0.1/admin/web。使用该路由器,您的目录不是可选的
  • 我弄错了它是127.0.0.1/user/index
  • 然后用正确的代码编辑你的答案,因为我不知道你在谈论用户或管理员
  • 建议您使用多条路线,而不是尝试使用通用的包罗万象的路线来解决您的问题。
  • 是的,它适用于多条路线。谢谢

标签: php kohana routes


【解决方案1】:

此路由将转换为:http://127.0.0.1/admin/web,但您的 Admin 文件夹内需要有 user 控制器。

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

如果您希望目录是可选的,则需要

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

但是,就您而言,您需要多条路线。在“catch all”路线上方,放上:

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',
));

【讨论】:

  • 使用默认路由 Route::set('default', '((/(/)))') ->defaults(array( 'controller' => '用户', '动作' => '索引', ));一切正常。现在我想添加 Admin 目录,这样当管理员输入 127.0.0.1/admin/dashboard/login 时,他会被定向到 admin 文件夹内的控制器,如果是简单的 User ,他可以通过默认的 127.0.0.1/user/login 直接访问。 User 的文件在 classes/controller/ 不是子目录中
  • 哦好@pocesar 多条路线,这似乎解决了我的问题。非常感谢
【解决方案2】:

尝试在-&gt;defaults中插入目录

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

【讨论】:

  • No @JGSilve,通过将目录插入到 ->defaults 它不起作用。事实上,这样做会告诉 Kohana User.php 在 admin 目录下。不是这种情况。正如我拟定的那样,在 Controller 文件夹下有 admin 文件夹(其中包含 Dashboard.php)和 User.php
猜你喜欢
  • 2012-01-21
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2011-10-01
  • 1970-01-01
  • 2012-11-04
相关资源
最近更新 更多