关闭 Kohana 3 中的默认路由?

发布于 2024-10-25 06:22:46 字数 531 浏览 4 评论 0 原文

我相信我知道如何做到这一点,但想向我出色的社区窥视者进行验证。 =)

这是一个例子:

我有一个名为“tami”的控制器类,其操作为“index”。

我知道,如果我希望有人通过“/tami/”或“/tami/index”以外的 URL 访问该控制器/操作组合,那么我应该通过如下方式添加一条路由:

Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
    ->defaults(array(
        'controller' => 'tami',
        'action' => 'index',
    ));

但是,用户仍然可以访问此页面来自 /tami/

如何关闭默认路由,以便我定义的路由唯一有效?

我假设我可以删除在 kohana/application/bootstrap.php 中找到的默认路由。这是正确的吗?或者这会破坏其他东西吗?

I believe I know how to do this, but wanted to verify with my awesome community peeps. =)

Here's an example:

I have a Controller class called 'tami', with an action 'index'.

I know that if I want someone to access that controller/action combo via an URL other than "/tami/" or "/tami/index", then I should add a route, via something like this:

Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
    ->defaults(array(
        'controller' => 'tami',
        'action' => 'index',
    ));

But, users can still access this page via /tami/.

How can I turn off the default routing, so that the only valid routes are the ones I define?

I assume I can just remove the default route found in kohana/application/bootstrap.php. Is that correct? Or would that break something else?

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

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

发布评论

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

评论(3

女皇必胜 2024-11-01 06:22:46

我想说的与 @simshaun 完全相同 - 要么删除默认路由(使其他控制器无法访问),要么检查 Controller_Tami 中的 before() 函数以获取 uri看看这是否是您所追求的。

如果您使用的是 Kohana 3.1,您现在可以使用 lambda 逻辑/匿名函数定义您的路线

像这样的东西会从控制器中取出额外的路由逻辑(这很好,因为我们将其保存在一个地方):

Route::set('default', function($uri)
    {
        if ($uri == 'tami' OR $uri == 'tami/index')
        {
            // Route not allowed by the default methods
            throw new Kohana_404_Exception("Route not permitted");
        }
    },
    '(<controller>(/<action>(/<id>)))'
);

我还没有使用过的东西,但它看起来非常强大。

I'd say exactly the same as @simshaun — either remove the default route (leaving other controllers unreachable) or check in the before() function in Controller_Tami for the uri to see if it's what you're after.

If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes.

Something like this would take the extra routing logic out of the controller (which is good as we're keeping it in one place):

Route::set('default', function($uri)
    {
        if ($uri == 'tami' OR $uri == 'tami/index')
        {
            // Route not allowed by the default methods
            throw new Kohana_404_Exception("Route not permitted");
        }
    },
    '(<controller>(/<action>(/<id>)))'
);

Something I haven't yet used but it looks amazingly powerful.

べ映画 2024-11-01 06:22:46

我认为最简单的方法是删除引导文件中的默认路由,是的。但是,您未手动指定路由的任何控制器将无法再访问。

我要做的是创建一个类,例如 Controller_Tami 扩展的 Controller_Derouter 。使用 Controller_Derouter 中的 before() 方法来测试是否从默认路由访问控制器,如果是,则抛出 404。我认为您应该能够通过比较 $ 来做到这一点this->request->controller 针对第一个 URI 段。

编辑:如果您只计划禁用 Tami 控制器的默认路由,则上述解决方案是不必要的。如果是这种情况,您可以直接在 Tami 控制器中实现 before() 方法。

I think the easiest way would be to remove the default route in your bootstrap file, yes. However, any controllers that you have not manually specified a route for can no longer be accessed.

What I would do is create a class, e.g. Controller_Derouter that Controller_Tami extends. Use the before() method in Controller_Derouter to test if the controller was accessed from the default route, and if so, throw a 404. I think you should be able to do that by comparing $this->request->controller against the first URI segment.

Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. If that's the case, you could just implement the before() method directly in the Tami controller.

眼角的笑意。 2024-11-01 06:22:46

也许像这样?

Route::set('pretty_tami', 'these-are-my-initials/<action>')
    ->defaults(array(
        'controller' => 'tami',
    ));

因此不会有默认的操作。您可能想使用正则表达式更新 default 路由(如果您还有的话)以排除 tami

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

Maybe like this?

Route::set('pretty_tami', 'these-are-my-initials/<action>')
    ->defaults(array(
        'controller' => 'tami',
    ));

So there wouldn't be a default action. And you probably want to update the default route (if you still have one) with a regex to exclude tami.

Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
    ->defaults(array(
        'controller' => 'welcome',
        'action'     => 'index',
    ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文