I’m testing ZF2 beta4 at the moment, and I’m having some issues accessing some of the request properties from my controller.
From inside the indexAction(), here is an extract of what I get for the request object
var_dump($this->request); gives me:
object(Zend\Http\PhpEnvironment\Request)#119 (14) {
["baseUrl":protected]=>
string(0) ""
[...]
["version":protected]=>
string(3) "1.1"
["queryParams":protected]=>
object(Zend\Stdlib\Parameters)#122 (1) {
["storage":"ArrayObject":private]=>
array(3) {
["q"]=>
string(0) ""
["param1"]=>
string(6) "value1"
["param2"]=>
string(6) "value2"
}
}
I can access some of the properties:
var_dump($this->request->getBaseUrl()); -> string(0) ""
var_dump($this->request->getVersion()); -> string(3) "1.1"
But I am having issues with others, for instance $this->request->getQueryParams() returns:
Call to undefined method
Zend\Http\PhpEnvironment\Request::getQueryParams()
From there I have a few questions:
-Am I doing something wrong?
If not:
-How can I access the queryParams property?
-Is it common practice not to have methods to access certain properties (if so what’s the point of having these properties if I can’t access them) ?
All the properties you dumped are protected, it’s why you needed a getter to fetch their values.
Existing getter for some properties don’t mean that they exist for all 🙂
If you take a look at the source of
Zend\Http\Request(which is inherited byZend\Http\PhpEnvironment\Request) you can see that the method name used to access the query param isquery()Note: I agree that’s a bit inconsistent.
Warning: It has been changed since the beta5 and it is now
getQuery()Protected and private members are used to ensure that the data won’t be accessed directly.
It is mostly used for internal use only, or provided with a public setter/getter to allow transformation, error checking, etc.
For example, you may want to add type hinting.