Kohana 3路由和查询字符串 [英] Kohana 3 Routing and Query Strings

查看:99
本文介绍了Kohana 3路由和查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的路线:

Route::set('test', 'test')
    ->defaults(array(
        'controller' => 'test',
        'action' => 'index'
    ));

我假设只会发送网址mysite.com/test或mysite.com/test/到此路由,其他任何内容都将发送到默认路由,如果有,则将捕获所有路由。但是,您可以附加任何查询字符串,它仍然有效。例如,以下任何一种方法都可以正常工作:

I assumed that only the url mysite.com/test or mysite.com/test/ would be sent to this route and anything else would be sent to the default route, or a catch all route if you have one. However, you can tack on any query strings and it will still be valid. For example any of these would work:

mysite.com/test/?abc
mysite.com/test/?abc=123
mysite.com/test/?abc=123&blabla=lala

您基本上想要的任何东西。如何进行设置,以使测试路由与查询字符串的网址不匹配?另一个示例可能是:

Anything you want basically. How can I set it so that the test route doesn't match the URL with query strings? Another example might be this:

Route::set('test', 'test(/?order=<order>)', array('order' => 'title|date|author'))
    ->defaults(array(
        'controller' => 'test',
        'action' => 'index'
        'order' => 'title'
    ));

在此示例中,我假设唯一匹配此路由的URL是:

In this example, I would assume the only URLs to match this route would be:

mysite.com/test/?order=title
mysite.com/test/?order=date
mysite.com/test/?order=author

但是像以前一样,您可以添加任何其他查询字符串您想要的。

But like before, you can just add on any other query string you want.

是否有任何方法可以将这些无效的查询字符串传递到捕获所有路由,并将它们发送到404页面?还是我真的必须检查所有控制器并检查$ _GET并确保它们确实存在?

Is there any way to have these invalid query strings pass to the catch all route where they will be sent to a 404 page? Or do I literally have to go through all my controllers and perform a check on the $_GET and make sure they actually exist?

推荐答案

您不应使用路由来访问查询参数。

You're not supposed to access query parameters using your routes.

路由与查询字符串完全隔离,请勿尝试使用它们,因为您将使用mod_rewrite。要访问查询参数,您应该使用:

Routes are totally isolated from the query string, don't try to use them as you'd use mod_rewrite. To access query params you should use:

$order = $this->request->query('order');

这篇关于Kohana 3路由和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆