Zend_Controller-Request.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.request">
  4. <title>The Request Object</title>
  5. <sect2 id="zend.controller.request.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The request object is a simple value object that is passed between
  9. <classname>Zend_Controller_Front</classname> and the router, dispatcher, and
  10. controller classes. It packages the names of the requested module,
  11. controller, action, and optional parameters, as well as the rest of
  12. the request environment, be it <acronym>HTTP</acronym>, the <acronym>CLI</acronym>, or
  13. <acronym>PHP</acronym>-GTK.
  14. </para>
  15. <itemizedlist>
  16. <listitem><para>
  17. The module name is accessed by
  18. <methodname>getModuleName()</methodname> and
  19. <methodname>setModuleName()</methodname>.
  20. </para></listitem>
  21. <listitem><para>
  22. The controller name is accessed by
  23. <methodname>getControllerName()</methodname> and
  24. <methodname>setControllerName()</methodname>.
  25. </para></listitem>
  26. <listitem><para>
  27. The name of the action to call within that controller is
  28. accessed by <methodname>getActionName()</methodname> and
  29. <methodname>setActionName()</methodname>.
  30. </para></listitem>
  31. <listitem><para>
  32. Parameters to be accessible by the action are an associative array
  33. of key and value pairs that are retrieved by <methodname>getParams()</methodname>
  34. and set with <methodname>setParams()</methodname>, or individually by
  35. <methodname>getParam()</methodname> and <methodname>setParam()</methodname>.
  36. </para></listitem>
  37. </itemizedlist>
  38. <para>
  39. Based on the type of request, there may be more methods available.
  40. The default request used, <classname>Zend_Controller_Request_Http</classname>,
  41. for instance, has methods for retrieving the request <acronym>URI</acronym>, path
  42. information, <varname>$_GET</varname> and <varname>$_POST</varname> parameters,
  43. etc.
  44. </para>
  45. <para>
  46. The request object is passed to the front controller, or if none is
  47. provided, it is instantiated at the beginning of the dispatch
  48. process, before routing occurs. It is passed through to every object
  49. in the dispatch chain.
  50. </para>
  51. <para>
  52. Additionally, the request object is particularly useful in testing.
  53. The developer may craft the request environment, including module,
  54. controller, action, parameters, <acronym>URI</acronym>, etc, and pass the request
  55. object to the front controller to test application flow. When paired
  56. with the <link linkend="zend.controller.response">response
  57. object</link>, elaborate and precise unit testing of <acronym>MVC</acronym>
  58. applications becomes possible.
  59. </para>
  60. </sect2>
  61. <sect2 id="zend.controller.request.http">
  62. <title>HTTP Requests</title>
  63. <sect3 id="zend.controller.request.http.dataacess">
  64. <title>Accessing Request Data</title>
  65. <para>
  66. <classname>Zend_Controller_Request_Http</classname> encapsulates access to
  67. relevant values such as the key name and value for the
  68. controller and action router variables, and all additional
  69. parameters parsed from the <acronym>URI</acronym>. It additionally allows access to
  70. values contained in the superglobals as public members, and
  71. manages the current Base <acronym>URL</acronym> and Request <acronym>URI</acronym>.
  72. Superglobal values cannot be set on a request object, instead use the
  73. <methodname>setParam()</methodname> and <methodname>getParam()</methodname> methods
  74. to set or retrieve user parameters.
  75. </para>
  76. <note>
  77. <title>Superglobal Data</title>
  78. <para>
  79. When accessing superglobal data through
  80. <classname>Zend_Controller_Request_Http</classname> as public member
  81. properties, it is necessary to keep in mind that the
  82. property name (superglobal array key) is matched to a
  83. superglobal in a specific order of precedence: 1. <constant>GET</constant>, 2.
  84. <constant>POST</constant>, 3. <constant>COOKIE</constant>, 4.
  85. <constant>SERVER</constant>, 5. <constant>ENV</constant>.
  86. </para>
  87. </note>
  88. <para>
  89. Specific superglobals can be accessed using a public method as
  90. an alternative. For example, the raw value of
  91. <varname>$_POST['user']</varname> can be accessed by calling
  92. <methodname>getPost('user')</methodname> on the request object. These
  93. include <methodname>getQuery()</methodname> for retrieving
  94. <varname>$_GET</varname> elements, and <methodname>getHeader()</methodname> for
  95. retrieving request headers.
  96. </para>
  97. <note>
  98. <title>GET and POST Data</title>
  99. <para>
  100. Be cautious when accessing data from the request object as
  101. it is not filtered in any way. The router and dispatcher
  102. validate and filter data for use with their tasks, but leave
  103. the data untouched in the request object.
  104. </para>
  105. </note>
  106. <note>
  107. <title>Retrieving the Raw POST Data</title>
  108. <para>
  109. As of 1.5.0, you can also retrieve the raw post data via the
  110. <methodname>getRawBody()</methodname> method. This method returns
  111. <constant>FALSE</constant> if no data was submitted in that fashion, but the
  112. full body of the post otherwise.
  113. </para>
  114. <para>
  115. This is primarily useful for accepting content when
  116. developing a RESTful <acronym>MVC</acronym> application.
  117. </para>
  118. </note>
  119. <para>
  120. You may also set user parameters in the request object using
  121. <methodname>setParam()</methodname> and retrieve these later using
  122. <methodname>getParam()</methodname>. The router makes use of this
  123. functionality to set parameters matched in the request <acronym>URI</acronym> into
  124. the request object.
  125. </para>
  126. <note>
  127. <title>getParam() Retrieves More than User Parameters</title>
  128. <para>
  129. In order to do some of its work, <methodname>getParam()</methodname> actually
  130. retrieves from several sources. In order of priority, these
  131. include: user parameters set via <methodname>setParam()</methodname>,
  132. <constant>GET</constant> parameters, and finally <constant>POST</constant>
  133. parameters. Be aware of this when pulling data via this
  134. method.
  135. </para>
  136. <para>
  137. If you wish to pull only from parameters you set via
  138. <methodname>setParam()</methodname>, use the
  139. <methodname>getUserParam()</methodname>.
  140. </para>
  141. <para>
  142. Additionally, as of 1.5.0, you can lock down which parameter
  143. sources will be searched. <methodname>setParamSources()</methodname>
  144. allows you to specify an empty array or an array with one or
  145. more of the values '_GET' or '_POST' indicating which
  146. parameter sources are allowed (by default, both are
  147. allowed); if you wish to restrict access to only '_GET'
  148. specify <methodname>setParamSources(array('_GET'))</methodname>.
  149. </para>
  150. </note>
  151. <note>
  152. <title>Apache Quirks</title>
  153. <para>
  154. If you are using Apache's 404 handler to pass incoming
  155. requests to the front controller, or using a PT flag with
  156. rewrite rules, <varname>$_SERVER['REDIRECT_URL']</varname>
  157. contains the <acronym>URI</acronym> you need, not
  158. <varname>$_SERVER['REQUEST_URI']</varname>. If you are using such
  159. a setup and getting invalid routing, you should use the
  160. <classname>Zend_Controller_Request_Apache404</classname> class instead
  161. of the default Http class for your request object:
  162. </para>
  163. <programlisting language="php"><![CDATA[
  164. $request = new Zend_Controller_Request_Apache404();
  165. $front->setRequest($request);
  166. ]]></programlisting>
  167. <para>
  168. This class extends the
  169. <classname>Zend_Controller_Request_Http</classname> class and simply
  170. modifies the autodiscovery of the request <acronym>URI</acronym>. It can be
  171. used as a drop-in replacement.
  172. </para>
  173. </note>
  174. </sect3>
  175. <sect3 id="zend.controller.request.http.baseurl">
  176. <title>Base Url and Subdirectories</title>
  177. <para>
  178. <classname>Zend_Controller_Request_Http</classname> allows
  179. <classname>Zend_Controller_Router_Rewrite</classname> to be used in subdirectories.
  180. <classname>Zend_Controller_Request_Http</classname> will attempt to automatically
  181. detect your base <acronym>URL</acronym> and set it accordingly.
  182. </para>
  183. <para>
  184. For example, if you keep your <filename>index.php</filename> in a
  185. webserver subdirectory named
  186. <filename>/projects/myapp/index.php</filename>, base <acronym>URL</acronym> (rewrite
  187. base) should be set to <filename>/projects/myapp</filename>. This string will
  188. then be stripped from the beginning of the path before
  189. calculating any route matches. This frees one from the necessity
  190. of prepending it to any of your routes. A route of
  191. <command>'user/:username'</command> will match <acronym>URI</acronym>s like
  192. <filename>http://localhost/projects/myapp/user/martel</filename> and
  193. <filename>http://example.com/user/martel</filename>.
  194. </para>
  195. <note>
  196. <title>URL Detection is Case Sensitive</title>
  197. <para>
  198. Automatic base <acronym>URL</acronym> detection is case sensitive, so make sure
  199. your <acronym>URL</acronym> will match a subdirectory name in a filesystem (even
  200. on Windows machines). If it doesn't, an exception will be raised.
  201. </para>
  202. </note>
  203. <para>
  204. Should base <acronym>URL</acronym> be detected incorrectly you can override it
  205. with your own base path with the help of the
  206. <methodname>setBaseUrl()</methodname> method of either the
  207. <classname>Zend_Controller_Request_Http</classname> class, or the
  208. <classname>Zend_Controller_Front</classname> class. The easiest
  209. method is to set it in <classname>Zend_Controller_Front</classname>,
  210. which will proxy it into the request object. Example usage to
  211. set a custom base <acronym>URL</acronym>:
  212. </para>
  213. <programlisting language="php"><![CDATA[
  214. /**
  215. * Dispatch Request with custom base URL with Zend_Controller_Front.
  216. */
  217. $router = new Zend_Controller_Router_Rewrite();
  218. $controller = Zend_Controller_Front::getInstance();
  219. $controller->setControllerDirectory('./application/controllers')
  220. ->setRouter($router)
  221. ->setBaseUrl('/projects/myapp'); // set the base url!
  222. $response = $controller->dispatch();
  223. ]]></programlisting>
  224. </sect3>
  225. <sect3 id="zend.controller.request.http.method">
  226. <title>Determining the Request Method</title>
  227. <para>
  228. <methodname>getMethod()</methodname> allows you to determine the
  229. <acronym>HTTP</acronym> request method used to request the current resource.
  230. Additionally, a variety of methods exist that allow you to get
  231. boolean responses when asking if a specific type of request has
  232. been made:
  233. </para>
  234. <itemizedlist>
  235. <listitem><para><methodname>isGet()</methodname></para></listitem>
  236. <listitem><para><methodname>isPost()</methodname></para></listitem>
  237. <listitem><para><methodname>isPut()</methodname></para></listitem>
  238. <listitem><para><methodname>isDelete()</methodname></para></listitem>
  239. <listitem><para><methodname>isHead()</methodname></para></listitem>
  240. <listitem><para><methodname>isOptions()</methodname></para></listitem>
  241. </itemizedlist>
  242. <para>
  243. The primary use case for these is for creating RESTful <acronym>MVC</acronym>
  244. architectures.
  245. </para>
  246. </sect3>
  247. <sect3 id="zend.controller.request.http.ajax">
  248. <title>Detecting AJAX Requests</title>
  249. <para>
  250. <classname>Zend_Controller_Request_Http</classname> has a rudimentary
  251. method for detecting <acronym>AJAX</acronym> requests:
  252. <methodname>isXmlHttpRequest()</methodname>. This method looks for an
  253. <acronym>HTTP</acronym> request header <emphasis>X-Requested-With</emphasis> with
  254. the value 'XMLHttpRequest'; if found, it returns <constant>TRUE</constant>.
  255. </para>
  256. <para>
  257. Currently, this header is known to be passed by default with the
  258. following JS libraries:
  259. </para>
  260. <itemizedlist>
  261. <listitem><para>Prototype and Scriptaculous (and libraries derived
  262. from Prototype)</para></listitem>
  263. <listitem><para>Yahoo! UI Library</para></listitem>
  264. <listitem><para>jQuery</para></listitem>
  265. <listitem><para>MochiKit</para></listitem>
  266. </itemizedlist>
  267. <para>
  268. Most <acronym>AJAX</acronym> libraries allow you to send custom
  269. <acronym>HTTP</acronym> request headers; if your library does not send this header,
  270. simply add it as a request header to ensure the
  271. <methodname>isXmlHttpRequest()</methodname> method works for you.
  272. </para>
  273. </sect3>
  274. </sect2>
  275. <sect2 id="zend.controller.request.subclassing">
  276. <title>Subclassing the Request Object</title>
  277. <para>
  278. The base request class used for all request objects is the abstract
  279. class <classname>Zend_Controller_Request_Abstract</classname>. At its most
  280. basic, it defines the following methods:
  281. </para>
  282. <programlisting language="php"><![CDATA[
  283. abstract class Zend_Controller_Request_Abstract
  284. {
  285. /**
  286. * @return string
  287. */
  288. public function getControllerName();
  289. /**
  290. * @param string $value
  291. * @return self
  292. */
  293. public function setControllerName($value);
  294. /**
  295. * @return string
  296. */
  297. public function getActionName();
  298. /**
  299. * @param string $value
  300. * @return self
  301. */
  302. public function setActionName($value);
  303. /**
  304. * @return string
  305. */
  306. public function getControllerKey();
  307. /**
  308. * @param string $key
  309. * @return self
  310. */
  311. public function setControllerKey($key);
  312. /**
  313. * @return string
  314. */
  315. public function getActionKey();
  316. /**
  317. * @param string $key
  318. * @return self
  319. */
  320. public function setActionKey($key);
  321. /**
  322. * @param string $key
  323. * @return mixed
  324. */
  325. public function getParam($key);
  326. /**
  327. * @param string $key
  328. * @param mixed $value
  329. * @return self
  330. */
  331. public function setParam($key, $value);
  332. /**
  333. * @return array
  334. */
  335. public function getParams();
  336. /**
  337. * @param array $array
  338. * @return self
  339. */
  340. public function setParams(array $array);
  341. /**
  342. * @param boolean $flag
  343. * @return self
  344. */
  345. public function setDispatched($flag = true);
  346. /**
  347. * @return boolean
  348. */
  349. public function isDispatched();
  350. }
  351. ]]></programlisting>
  352. <para>
  353. The request object is a container for the request environment. The
  354. controller chain really only needs to know how to set and retrieve the
  355. controller, action, optional parameters, and dispatched status. By
  356. default, the request will search its own parameters using the
  357. controller or action keys in order to determine the controller and
  358. action.
  359. </para>
  360. <para>
  361. Extend this class, or one of its derivatives, when you need the
  362. request class to interact with a specific environment in order to
  363. retrieve data for use in the above tasks. Examples include <link
  364. linkend="zend.controller.request.http">the <acronym>HTTP</acronym>
  365. environment</link>, a <acronym>CLI</acronym> environment, or a
  366. <acronym>PHP</acronym>-GTK environment.
  367. </para>
  368. </sect2>
  369. </sect1>
  370. <!--
  371. vim:se ts=4 sw=4 et:
  372. -->