View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Http_Request
{zone-data}

{zone-data:proposer-list}
[Michael Sheakoski|mailto:michael@-REMOVE-THIS-TEXT-mjsdigital.com]
{zone-data}

{zone-data:revision}
1.1 - 1 August 2006: Updated from community comments.
{zone-data}

{zone-data:overview}
* *Zend_Http_Request* is a container used for accessing GET, POST, COOKIE, PATH_INFO, and various input from the browser. It uses error correction and fallback methods to emulate certain variables when they are not set or not in the proper format.
{zone-data}

{zone-data:references}
http://msdn2.microsoft.com/en-us/library/system.web.httprequest.aspx
http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html
{zone-data}

{zone-data:requirements}
{zone-data}

{zone-data:dependencies}
{zone-data}

{zone-data:operation}
The component can be instantiated anywhere in the script. The "get" accessor methods will automatically attempt to gather the requested data. If not successful, null is returned. Some methods include a "set" accessor as well in case the matching "get" accessor needs to be overridden.
{zone-data}

{zone-data:milestones}
{zone-data}

{zone-data:class-list}
* Zend_Http_Request
{zone-data}

{zone-data:use-cases}
{code}
<?php

set_include_path(get_include_path() . PATH_SEPARATOR . 'path/to/library');

require_once 'Zend.php';
function __autoload($class) {
Zend::loadClass($class);
}

// Let's pretend the url is: http://mysite.com/test/index.php/foo/bar?username=michael&state=California
// and some POST vars: city = 'Wonkaland", state = 'OH', zip = '12345'

$request = new Zend_Http_Request();

echo $request->getPathInfo(); // '/foo/bar'
echo $request->PATH_INFO; // same as above, uses same function
echo $request->getServer('PATH_INFO'); // value taken directly from $_SERVER instead of being calculated.
// ideally it would be the same as above but that's wishful thinking

echo $request->getPost('city'); // 'Wonkaland'
echo $request->city; // same as above
echo $request->state; // 'California' -- the order of precedence is GET, POST, COOKIE, SERVER, ENV

echo $request->getQuery('username'); // 'michael'
echo $request->getQuery('blahBlahThisDoesNotExist'); // null

echo $request->getBaseUrl() . '/user/edit'; // '/test/index.php/user/edit' -- useful for generating URLs
echo $request->getBasePath() . '/images/zend.png'; // '/test/images/zend.php' -- useful for pointing to images/css/js in your views
{code}
{zone-data}

{zone-data:skeletons}
{composition-setup}{composition-setup}
{deck:id=Source}
{card:label=Zend_Http_Request}
h3. This code is shared with the Zend MVC Changes proposal http://framework.zend.com/wiki/x/eB
{code}
<?php

/** Zend_Request_Interface */
require_once 'Zend/Request/Interface.php';

class Zend_Http_Request implements Zend_Request_Interface
{
protected $_requestUri;
protected $_baseUrl;
protected $_basePath;
protected $_pathInfo;
protected $_params = array();
protected $_aliases = array();

/**
* Access values contained in the superglobals as public members
* Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
*
* http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if (isset($_GET[$key])) {
return $_GET[$key];
} elseif (isset($_POST[$key])) {
return $_POST[$key];
} elseif (isset($_COOKIE[$key])) {
return $_COOKIE[$key];
} elseif (isset($_SERVER[$key])) {
switch ($key) {
case 'REQUEST_URI' : return $this->getRequestUri();
case 'PATH_INFO' : return $this->getPathInfo();
default : return $_SERVER[$key];
}
} elseif (isset($_ENV[$key])) {
return $_ENV[$key];
}

return null;
}

public function getQuery($key)
{
return (isset($_GET[$key])) ? $_GET[$key] : null;
}

public function getPost($key)
{
return (isset($_POST[$key])) ? $_POST[$key] : null;
}

public function getCookie($key)
{
return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : null;
}

public function getServer($key)
{
return (isset($_SERVER[$key])) ? $_SERVER[$key] : null;
}

public function getEnv($key)
{
return (isset($_ENV[$key])) ? $_ENV[$key] : null;
}

public function setRequestUri($requestUri = null)
{
if ($requestUri === null) {
if (isset($_SERVER['REQUEST_URI'])) {
$requestUri = $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
} else {
return false;
}
}

$this->_requestUri = $requestUri;
}

/**
* Returns the REQUEST_URI taking into account
* platform differences between Apache and IIS
*
* @return string
*/
public function getRequestUri()
{
if ($this->_requestUri === null) {
$this->setRequestUri();
}

return $this->_requestUri;
}

public function setBaseUrl($baseUrl = null)
{
if ($baseUrl === null) {
$filename = basename($_SERVER['SCRIPT_FILENAME']);

if (basename($_SERVER['SCRIPT_NAME']) === $filename) {
$baseUrl = $_SERVER['SCRIPT_NAME'];
} elseif (basename($_SERVER['PHP_SELF']) === $filename) {
$baseUrl = $_SERVER['PHP_SELF'];
} elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
$baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
} else {
return false;
}

if (($requestUri = $this->getRequestUri()) === null) {
return false;
}

// If using mod_rewrite or ISAPI_Rewrite strip the script filename out of baseUrl
// $pos !== 0 makes sure it is not matching a value from PATH_INFO or QueryString
if (($pos = strpos($requestUri, $baseUrl)) === false || $pos !== 0) {
$baseUrl = dirname($baseUrl);
}
}

$this->_baseUrl = rtrim($baseUrl, '/');
}

/**
* Everything in REQUEST_URI before PATH_INFO
* <form action="<?=$baseUrl?>/news/submit" method="POST"/>
*
* @return string
*/
public function getBaseUrl()
{
if ($this->_baseUrl === null) {
$this->setBaseUrl();
}

return $this->_baseUrl;
}

public function setBasePath($basePath = null)
{
if ($basePath === null) {
$filename = basename($_SERVER['SCRIPT_FILENAME']);

if (($baseUrl = $this->getBaseUrl()) === null) {
return false;
}

if (basename($baseUrl) === $filename) {
$basePath = dirname($baseUrl);
} else {
$basePath = $baseUrl;
}
}

$this->_basePath = rtrim($basePath, '/');
}

/**
* Everything in REQUEST_URI before PATH_INFO not including the filename
* <img src="<?=$basePath?>/images/zend.png"/>
*
* @return string
*/
public function getBasePath()
{
if ($this->_basePath === null) {
$this->setBasePath();
}

return $this->_basePath;
}

public function setPathInfo($pathInfo = null)
{
if ($pathInfo === null) {
if (($baseUrl = $this->getBaseUrl()) === null) {
return false;
}

if (($requestUri = $this->getRequestUri()) === null) {
return false;
}

// Remove the query string from REQUEST_URI
if ($pos = strpos($requestUri, '?')) {
$requestUri = substr($requestUri, 0, $pos);
}

if (($pathInfo = substr($requestUri, strlen($baseUrl))) === false) {
// If substr() returns false then PATH_INFO is set to an empty string
$pathInfo = '';
}
}

$this->_pathInfo = $pathInfo;
}

/**
* Returns everything between the BaseUrl and QueryString.
* This value is calculated instead of reading PATH_INFO
* directly from $_SERVER due to cross-platform differences.
*
* @return string
*/
public function getPathInfo()
{
if ($this->_pathInfo === null) {
$this->setPathInfo();
}

return $this->_pathInfo;
}

public function setParam($key, $value)
{
$keyName = (($alias = $this->getAlias($key)) !== null) ? $alias : $key;
$this->_params[$keyName] = $value;
}

public function getParam($key)
{
$keyName = (($alias = $this->getAlias($key)) !== null) ? $alias : $key;

if (isset($this->_params[$keyName])) {
return $this->_params[$keyName];
} elseif ((isset($_GET[$keyName]))) {
return $_GET[$keyName];
} elseif ((isset($_POST[$keyName]))) {
return $_POST[$keyName];
}

return null;
}

public function getParams()
{
return $this->_params + $_GET + $_POST;
}

public function setParams(array $params)
{
foreach ($params as $key => $value) {
$this->setParam($key, $value);
}
}

public function setAlias($name, $target)
{
$this->_aliases[$name] = $target;
}

public function getAlias($name)
{
if (isset($this->_aliases[$name])) {
return $this->_aliases[$name];
}

return null;
}

public function getAliases()
{
return $this->_aliases;
}
}
{code}
{card}
{card:label=Zend_Request_Interface}
{code}
<?php

interface Zend_Request_Interface
{
public function setParam($key, $value);
public function getParam($key);
public function getParams();
public function setParams(array $params);
}
{code}
{card}
{deck}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>