Kohana 3 Routing and Query Strings

久未见 提交于 2019-12-13 18:22:43

问题


If I have a route like this:

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

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'
    ));

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.

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?


回答1:


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

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');


来源:https://stackoverflow.com/questions/4475351/kohana-3-routing-and-query-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!