开发者

What is the correct way to get values from the query string in Kohana 3

Just curious as to what is the 'Kohana' way of getting variables from开发者_运维知识库 the query string?

The best that I could come up with is parsing the $_GET var with the Arr class. Anybody have a better way to do this?

// foo?a=1&b=2
function action_welcome()
{
    echo('a = '.Arr::get($_GET, 'a', '0'));
    echo('b = '.Arr::get($_GET, 'b', '0'));
}


I think using Arr::get is too general, it is more practical to use specific Kohana method designed exactly for this

Request::current->query('variable')

or

$this->request->query('variable')

even the request is internal you can have any variables passed to it


That's pretty much the right way, I'd only suggest you to use NULL as default instead of string '0' where ever you can.

You can also use this function for any kind of array, not only global vars, so instead of

$var = isset($arr['key']) ? $array['key'] : NULL

you just do (Kohana 3.0)

$var = Arr::get($arr, 'key', NULL);

or (Kohana 3.1+)

$var = $request->query('key');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜