[英]Turn off default routes in Kohana 3?
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'. 我有一个名为“ tami”的Controller类,带有一个动作“ 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: 我知道,如果我希望有人通过除“ / tami /”或“ / tami / index”以外的URL访问该控制器/操作组合,那么我应该通过以下方式添加一条路由:
Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
->defaults(array(
'controller' => 'tami',
'action' => 'index',
));
But, users can still access this page via /tami/
. 但是,用户仍然可以通过
/tami/
访问此页面。
I assume I can just remove the default route found in kohana/application/bootstrap.php
. 我假设我可以删除在
kohana/application/bootstrap.php
找到的默认路由。 Is that correct? 那是对的吗? Or would that break something else?
还是会破坏其他东西?
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, eg Controller_Derouter
that Controller_Tami extends. 我会做的是创建一个类,如
Controller_Derouter
那Controller_Tami延伸。 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. 使用Controller_Derouter中的
before()
方法测试是否通过默认路由访问了控制器,如果是,则抛出404。我认为您应该可以通过将$this->request->controller
与第一个进行比较来做到这一点。 URI段。
Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. 编辑:如果仅计划仅禁用Tami控制器的默认路由,则上述解决方案是不必要的。 If that's the case, you could just implement the before() method directly in the Tami controller.
如果是这种情况,您可以直接在Tami控制器中实现before()方法。
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. 我想说的与@simshaun完全一样-删除默认路由(使其他控制器无法访问),或者在
Controller_Tami
检查before()
函数中的uri,以查看它是否在后面。
If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes . 如果您使用的是Kohana 3.1, 则现在可以使用lambda逻辑/匿名函数来定义路线 。
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. 我尚未使用过的东西,但看起来功能强大。
Maybe like this? 也许是这样的?
Route::set('pretty_tami', 'these-are-my-initials/<action>')
->defaults(array(
'controller' => 'tami',
));
So there wouldn't be a default action
. 因此,不会有默认
action
。 And you probably want to update the default
route (if you still have one) with a regex to exclude tami
. 您可能想用正则表达式更新
default
路由(如果仍然有)以排除tami
。
Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.