Joomla! provides us with the static class
JRequest, which eliminates the need to directly access the request hashes $_GET,
$_POST, $_FILES, $_COOKIE, and $_REQUEST. Using JRequest to its full potential we
can perform useful data preprocessing.
Preprocessing CGI Data
To access a request value we must use the static JRequest::getVar() method. In
this example we get the value of the input id:
$id = JRequest::getVar('id');
If we want to, we can define a default value; this is the value that will be returned if
the request value is not defined. In this example we use the value 0 if the request id
is not set:
$id = JRequest::getVar('id', 0);
By default JRequest::getVar() obtains data from the $_REQUEST hash. We can
specify the source hash of the data as any one of the following: GET, POST, FILES,
COOKIE, and DEFAULT. If we specify DEFAULT or an unknown source hash, the data
will be retrieved from the $_REQUEST hash. In this example we get the data from
the $_POST hash:
$id = JRequest::getVar('id', 0, 'POST');
Casting is a mechanism we can use to guarantee that a variable is of a specific type.
Pages:
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432