The request object in Zend Framework makes it easy to access URI and environment information anywhere in the application.php
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:web
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" } |
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:app
1 2 3 4 5 6 |
$this->getRequest()->getRequestUri(); // URL project.local/mycontroller/myaction?paramkey=paramvalue // Result /mycontroller/myaction?paramkey=paramval |
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" } |
1 2 3 4 |
$this->getRequest()->getScheme(); // Result 'http' or 'https' |
1 2 3 4 |
$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.ide
1 2 3 |
$request = $this->getRequest(); echo $request->getScheme() . '://' . $request->getHttpHost() . $request->getRequestUri(); |
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.ui
1 2 |
$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!this
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.url
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); |