我相信我知道如何做到这一点,但想向我出色的社区窥视者进行验证。 =)
这是一个例子:
我有一个名为“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?
发布评论
评论(3)
我想说的与 @simshaun 完全相同 - 要么删除默认路由(使其他控制器无法访问),要么检查
Controller_Tami
中的before()
函数以获取 uri看看这是否是您所追求的。如果您使用的是 Kohana 3.1,您现在可以使用 lambda 逻辑/匿名函数定义您的路线。
像这样的东西会从控制器中取出额外的路由逻辑(这很好,因为我们将其保存在一个地方):
我还没有使用过的东西,但它看起来非常强大。
I'd say exactly the same as @simshaun — either remove the default route (leaving other controllers unreachable) or check in the
before()
function inController_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):
Something I haven't yet used but it looks amazingly powerful.
我认为最简单的方法是删除引导文件中的默认路由,是的。但是,您未手动指定路由的任何控制器将无法再访问。
我要做的是创建一个类,例如 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 thebefore()
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.
也许像这样?
因此不会有默认的
操作
。您可能想使用正则表达式更新default
路由(如果您还有的话)以排除tami
。Maybe like this?
So there wouldn't be a default
action
. And you probably want to update thedefault
route (if you still have one) with a regex to excludetami
.