带有默认子目录的 Kohana 3.1 路由

发布于 2024-12-01 14:13:17 字数 2068 浏览 0 评论 0原文

我有一个应用程序,在我尝试实现用于登录和注册的身份验证模块之前,该应用程序基本上可以正常工作。

我的应用程序目录结构基本上是:

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

默认情况下,我想要 URL,例如 http://www.testsite.com/test 它将访问 Controller_Page_Test 类。或者显式调用管理或阻止页面 http://www.testsite.com/admin/test它将访问 Controller_Admin_Test 类。更复杂的是,它还需要处理可选的操作和 ID。

我在顶部说过,这基本上工作正常 - 直到我尝试添加 Auth 模块。 Auth 模块调用 http://www.testsite.com/user/login 但不是访问模块的默认路径,它在页面目录中查找。

为了克服这个问题,我放置了一个更高级别的路由,但现在它已成为我的默认页面处理程序。显式调用仍然可以通过。

我的路线目前如下所示:

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

可以更好地清理吗?如何让该模块仅在我需要时启动?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2024-12-08 14:13:17

是的,可以更好地清洁。 Kohana 开发人员鼓励使用此框架的人们根据需要添加尽可能多的路由。您甚至可以为每个操作指定它们,这将使您将来能够更改 URL(例如,您可能希望使用 /signin 而不是 /user/login),如果您使用正确的方法来生成链接等(例如Route::url() helper)。

现在,这里是指定 user 路由的另一种方法:

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

它将仅匹配请求,其中 URI 的第一部分已给出并且等于 user代码>或<代码>admin_user。以前,控制器部分是可选的,因此也匹配对 / URI 的调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文