Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 15 ■ INTRODUCTION TO THE ZEND FRAMEWORK^225

The Request Object
The following five request object methods all work the same and allow you to interact with the
standard input streams:

getQuery($key = null, $default = null)
getPost($key = null, $default = null)
getCookie($key = null, $default = null)
getServer($key = null, $default = null)
getEnv($key = null, $default = null)

If you pass a null key parameter, you will get the entire array of information. If you pass a
key, you will get the value for that key only. You can also pass a default value so that if there is
no matching key, that value will be returned. In the case that no key is found and no default is
passed, null will be returned.
If you need to access the HTTP request headers, you will use the getHeader() method by
passing in the header name. Remember that HTTP headers are case-sensitive.

getHeader($header)

Parameters are a key part of any MVC implementation. They are an alternative to get variables
and make your sites more attractive to search engines. To use parameters in a Zend Framework
application, the URL is divided up like so:

http://example.com/controller/action/key1/value1/key2/value2...

By using parameters, you can pass critical query information, such as a customer ID, to
your scripts without using get information. Using parameters is one of the best ways to make
sure that a search engine spider will fetch your dynamic content, as most spiders ignore query
variables.
The getParam() method will also access query and post information if the requested key
cannot be found in the URL parameters.

getParam($key, $default = null)

The getParams() method returns an array of all parameters, and query and post information.

getParams()

To determine the current HTTP request method—GET, POST, HEAD, and so on—you can use
the following method:

getMethod()

Alternatively, you can use one of the is convenience functions:

isPost()

■Tip To obtain a reference to the current request object from within a controller/action context, you may
call $this->getRequest() on the controller instance.

McArthur_819-9C15.fm Page 225 Thursday, February 28, 2008 7:44 AM

Free download pdf