In Joomla!, instead of
directly using these, we use the static JRequest class. We use this because it allows
us to process the input at the same time as retrieving it, this decreases the amount of
code required and helps improve security.
The request hashes $_GET, $_POST, $_FILES, $_COOKIE, and $_REQUEST are still
available, and in cases where we are porting existing applications we need not
change the use of these hashes.
The two methods that we use the most are JRequest::setVar() and JRequest::
getVar(). As the names suggest, one accesses request-data and the other sets it. In
this example, we get the value of id; if id is not set, we return a default value, 0 (the
default value is optional).
$id = JRequest::getVar('id', 0);
Getting Started
[ 30 ]
The JRequest::setVar() method is used to set values in the request hashes. In
comparison to the JRequest::getVar() method, this method is used relatively
infrequently. It is most commonly used to set default values. For example, we might
want to set the default task in a component if it is not already selected:
JRequest::setVar('task', 'someDefaultTask');
A useful trick to guarantee that a variable is set is to use the two methods in
conjunction.
Pages:
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51