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

2

5

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'));
}
Elam answered 22/7, 2010 at 2:7 Comment(0)
C
6

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');
Coleoptile answered 22/7, 2010 at 17:25 Comment(2)
Passing NULL as the 3rd argument is superfluous.Squaw
@The Pixel Developer exactly, as it's the default value anyways; I was only referring to it as a better practice than passing '0' :)Coleoptile
O
7

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

Ossetia answered 15/6, 2011 at 20:26 Comment(0)
C
6

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');
Coleoptile answered 22/7, 2010 at 17:25 Comment(2)
Passing NULL as the 3rd argument is superfluous.Squaw
@The Pixel Developer exactly, as it's the default value anyways; I was only referring to it as a better practice than passing '0' :)Coleoptile

© 2022 - 2024 — McMap. All rights reserved.