Kohana3 中的路由查询字符串
我正在尝试路由网址 http://host/order?servertype=1&plan=1< /a> 在 bootstrap.php 中使用以下条目时
Route::set('order', 'order(/<action>)?servertype=<id1>&plan=<id2>', array('id1'=>'[0-9]+','id2'=>'[0-9]+'))
->defaults(array(
'controller' => 'order',
'action' => 'index',
));
出现错误:无法找到与 URI 匹配的路由。我做错了什么?
I am trying to route the url http://host/order?servertype=1&plan=1 with the following entry in bootstrap.php
Route::set('order', 'order(/<action>)?servertype=<id1>&plan=<id2>', array('id1'=>'[0-9]+','id2'=>'[0-9]+'))
->defaults(array(
'controller' => 'order',
'action' => 'index',
));
I am getting error:Unable to find a route to match the URI. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Kohana 的路由系统并非设计用于处理查询字符串(我相信大多数框架也是如此)。如果您确实需要以这种方式操作查询字符串,那么您必须使用 mod_rewrite 将逻辑从 Kohana 移出并移至 .htaccess 文件中。
然而,执行路由的首选方法是使用分层(路径式)URL 结构,然后使用查询字符串来获取无法分层表示的任何信息(然后可以通过 $_GET 从控制器直接访问这些信息) )。
这意味着使用像
http://host/order//
这样的 URL 结构和像这样的路由方案:但是,当使用这种类型的 URL 结构时,最好在 URL 中使用比纯数字 ID 更具描述性的标记。因此,您可能会考虑使用 "slugs" 来识别您的服务器类型和计划类型,这将给出您的更具可读性和 SEO 友好性的 URL,例如 http://host/order/server-type/plan。
Kohana's routing system is not designed to work with the query string (and the same is true for most frameworks, I believe). If you really need to manipulate the query string in this way, then you'll have to move the logic out of Kohana and into an .htaccess file, using mod_rewrite.
However, the preferred way to perform routing is to use a hierarchical (path-style) URL structure and then use the query string for any information that cannot be represented hierarhically (which can then be accessed directly from the controller via
$_GET
).This would mean using a URL structure like
http://host/order/<id1>/<id2>
and a routing scheme like:However, when using this type of URL structure, it's better to use more descriptive tokens in the URL than plain numeric IDs. So you might consider using "slugs" to identify your server types and plan types, which would give your more readable and SEO-friendlu URLs like
http://host/order/server-type/plan
.