The request object in Zend Framework makes it easy to access URI and environment information anywhere in the application. In this article you’ll find out how to do it in one of three places: the controller, view, and in your library files.
The Controller
Your controller will extend the Zend_Controller_Action class. This class has the getRequest() method used to get the current request. Do a dump of the request object to get an idea of what it contains:
Dump Request Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| Zend_Debug::dump($this->getRequest());
// Result
object(Zend_Controller_Request_Http)#21 (15) {
["_paramSources":protected] => array(2) {
[0] => string(4) "_GET"
[1] => string(5) "_POST"
}
["_requestUri":protected] => string(10) "/mycontroller/myaction"
["_baseUrl":protected] => string(0) ""
["_basePath":protected] => NULL
["_pathInfo":protected] => string(10) "/mycontroller/myaction"
["_params":protected] => array(3) {
["controller"] => string(4) "mycontroller"
["action"] => string(4) "myaction"
["module"] => string(7) "default"
}
["_rawBody":protected] => NULL
["_aliases":protected] => array(0) {
}
["_dispatched":protected] => bool(true)
["_module":protected] => string(7) "default"
["_moduleKey":protected] => string(6) "module"
["_controller":protected] => string(4) "mycontroller"
["_controllerKey":protected] => string(10) "controller"
["_action":protected] => string(4) "myaction"
["_actionKey":protected] => string(6) "action"
} |
Zend_Debug::dump($this->getRequest());
// Result
object(Zend_Controller_Request_Http)#21 (15) {
["_paramSources":protected] => array(2) {
[0] => string(4) "_GET"
[1] => string(5) "_POST"
}
["_requestUri":protected] => string(10) "/mycontroller/myaction"
["_baseUrl":protected] => string(0) ""
["_basePath":protected] => NULL
["_pathInfo":protected] => string(10) "/mycontroller/myaction"
["_params":protected] => array(3) {
["controller"] => string(4) "mycontroller"
["action"] => string(4) "myaction"
["module"] => string(7) "default"
}
["_rawBody":protected] => NULL
["_aliases":protected] => array(0) {
}
["_dispatched":protected] => bool(true)
["_module":protected] => string(7) "default"
["_moduleKey":protected] => string(6) "module"
["_controller":protected] => string(4) "mycontroller"
["_controllerKey":protected] => string(10) "controller"
["_action":protected] => string(4) "myaction"
["_actionKey":protected] => string(6) "action"
}
Notice the object type is Zend_Controller_Request_Http. From this object we are able to to access specific HTTP request information, including getting parameters:
getRequestUri()
1
2
3
4
5
6
| $this->getRequest()->getRequestUri();
// URL
project.local/mycontroller/myaction?paramkey=paramvalue
// Result
/mycontroller/myaction?paramkey=paramval |
$this->getRequest()->getRequestUri();
// URL
project.local/mycontroller/myaction?paramkey=paramvalue
// Result
/mycontroller/myaction?paramkey=paramval
getParams()
1
2
3
4
5
6
7
8
9
10
11
| $this->getRequest()->getParams();
// URL
project.local/mycontroller/myaction?paramkey1=paramvalue1¶mkey2=paramvalue2
// Result
array(5) {
["controller"] => string(4) "test"
["action"] => string(4) "test"
["module"] => string(7) "default"
["paramkey1"] => string(11) "paramvalue1"
["paramkey2"] => string(11) "paramvalue2" } |
$this->getRequest()->getParams();
// URL
project.local/mycontroller/myaction?paramkey1=paramvalue1¶mkey2=paramvalue2
// Result
array(5) {
["controller"] => string(4) "test"
["action"] => string(4) "test"
["module"] => string(7) "default"
["paramkey1"] => string(11) "paramvalue1"
["paramkey2"] => string(11) "paramvalue2" }
getScheme()
1
2
3
4
| $this->getRequest()->getScheme();
// Result
'http' or 'https' |
$this->getRequest()->getScheme();
// Result
'http' or 'https'
getHttpHost()
1
2
3
4
| $this->getRequest()->getHttpHost();
// Result
'project.local' |
$this->getRequest()->getHttpHost();
// Result
'project.local'
Even with all the above information we will have to piece together bits and pieces to build an absolute URL. We will need the scheme, HTTP host, and URI.
1
2
3
| $request = $this->getRequest();
echo $request->getScheme() . '://' . $request->getHttpHost() . $request->getRequestUri(); |
$request = $this->getRequest();
echo $request->getScheme() . '://' . $request->getHttpHost() . $request->getRequestUri();
The Library
In library files you won’t have direct access to the request object. Luckily it’s just as easy to get the request object in the library because Zend_Controller_Front implements a Singleton Pattern.
1
2
| $request = Zend_Controller_Front::getInstance()->getRequest();
$url = $request->getScheme() . '://' . $request->getHttpHost() ; |
$request = Zend_Controller_Front::getInstance()->getRequest();
$url = $request->getScheme() . '://' . $request->getHttpHost() ;
In this case we are getting an instance of the front controller that already exists, and then accessing the same methods we were as before. Simple!
The View
To follow MVC architecture, it is invalid to access the request object directly from a view. Instead developers have access to the ServerUrl view helper that is able to return the URL or URI.
1
2
3
4
5
| // Returns URL. Example: http://project.local
echo $this->serverUrl();
// Returns URI. Example: http://project.local/mycontroller/myaction
echo $this->serverUrl(true); |
// Returns URL. Example: http://project.local
echo $this->serverUrl();
// Returns URI. Example: http://project.local/mycontroller/myaction
echo $this->serverUrl(true);