You're familiar with the getRequestParameter('myparam')
method, used to retrieve the value of a request parameter by its name. As a matter of fact, this method is a proxy for a chain of calls to the request's parameter holder getRequest()->getParameter('myparam')
. The action class has access to the request object, called sfWebRequest
in symfony, and to all its methods, via the getRequest()
method. Table 6-1 lists the most useful sfWebRequest
methods.
Table 6-1 - Methods of the sfWebRequest
Object
Name | Function | Sample Output |
---|---|---|
Request Information | ||
getMethod() |
Request method | Returns sfRequest::GET or sfRequest::POST constants |
getMethodName() |
Request method name | 'POST' |
getHttpHeader('Server') |
Value of a given HTTP header | 'Apache/2.0.59 (Unix) DAV/2 PHP/5.1.6' |
getCookie('foo') |
Value of a named cookie | 'bar' |
isXmlHttpRequest() * |
Is it an Ajax request? | true |
isSecure() |
Is it an SSL request? | true |
Request Parameters | ||
hasParameter('foo') |
Is a parameter present in the request? | true |
getParameter('foo') |
Value of a named parameter | 'bar' |
getParameterHolder()->getAll() |
Array of all request parameters | |
URI-Related Information | ||
getUri() |
Full URI | 'http://localhost/myapp_dev.php/mymodule/myaction' |
getPathInfo() |
Path info | '/mymodule/myaction' |
getReferer() ** |
Referrer | 'http://localhost/myapp_dev.php/' |
getHost() |
Host name | 'localhost' |
getScriptName() |
Front controller path and name | 'myapp_dev.php' |
Client Browser Information | ||
getLanguages() |
Array of accepted languages | Array( [0] => fr [1] => fr_FR [2] => en_US [3] => en ) |
getCharsets() |
Array of accepted charsets | Array( [0] => ISO-8859-1 [1] => UTF-8 [2] => * ) |
getAcceptableContentTypes() |
Array of accepted content types | Array( [0] => text/xml [1] => text/html |
- Works with prototype, Prototype, Mootools, and jQuery
** Sometimes blocked by proxies
The sfActions
class offers a few proxies to access the request methods more quickly, as shown in Listing 6-15.
Listing 6-15 - Accessing the sfRequest
Object Methods from an Action
class mymoduleActions extends sfActions { public function executeIndex() { $hasFoo = $this->getRequest()->hasParameter('foo'); $hasFoo = $this->hasRequestParameter('foo'); // Shorter version $foo = $this->getRequest()->getParameter('foo'); $foo = $this->getRequestParameter('foo'); // Shorter version } }
For multipart requests to which users attach files, the sfWebRequest
object provides a means to access and move these files, as shown in Listing 6-16.
Listing 6-16 - The sfWebRequest
Object Knows How to Handle Attached Files
class mymoduleActions extends sfActions { public function executeUpload() { if ($this->getRequest()->hasFiles()) { foreach ($this->getRequest()->getFileNames() as $uploadedFile) { $fileName = $this->getRequest()->getFileName($uploadedFile); $fileSize = $this->getRequest()->getFileSize($uploadedFile); $fileType = $this->getRequest()->getFileType($uploadedFile); $fileError = $this->getRequest()->hasFileError($uploadedFile); $uploadDir = sfConfig::get('sf_upload_dir'); $this->getRequest()->moveFile($uploadedFile, $uploadDir.'/'.$fileName); } } } }
You don't have to worry about whether your server supports the $_SERVER
or the $_ENV
variables, or about default values or server-compatibility issues--the sfWebRequest
methods do it all for you. Besides, their names are so evident that you will no longer need to browse the PHP documentation to find out how to get information from the request.
Note The code above uses the $fileName
as it was when the file was uploaded. As there is a small chance that this gets exploited by sending files with malicious file names, you always should normalize the target filename or generate it.