Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'));
}
like image 679
Gerry Shaw Avatar asked Jul 22 '10 02:07

Gerry Shaw


2 Answers

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

like image 167
tipograf ieromonah Avatar answered Nov 18 '22 15:11

tipograf ieromonah


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');
like image 6
Kemo Avatar answered Nov 18 '22 17:11

Kemo