Zend_Controller-ActionHelpers-ContextSwitch.xml 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.actionhelpers.contextswitch">
  4. <title>ContextSwitch and AjaxContext</title>
  5. <para>
  6. The <emphasis>ContextSwitch</emphasis> action helper is intended for
  7. facilitating returning different response formats on request.
  8. The <emphasis>AjaxContext</emphasis> helper is a specialized version of
  9. <emphasis>ContextSwitch</emphasis> that facilitates returning responses
  10. to XmlHttpRequests.
  11. </para>
  12. <para>
  13. To enable either one, you must provide hinting in your controller as to
  14. what actions can respond to which contexts. If an incoming request
  15. indicates a valid context for the given action, the helper will then:
  16. </para>
  17. <itemizedlist>
  18. <listitem><para>
  19. Disable layouts, if enabled.
  20. </para></listitem>
  21. <listitem><para>
  22. Set an alternate view suffix, effectively requiring a separate
  23. view script for the context.
  24. </para></listitem>
  25. <listitem><para>
  26. Send appropriate response headers for the context desired.
  27. </para></listitem>
  28. <listitem><para>
  29. Optionally, call specified callbacks to setup the context and/or
  30. perform post-processing.
  31. </para></listitem>
  32. </itemizedlist>
  33. <para>
  34. As an example, let's consider the following controller:
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. class NewsController extends Zend_Controller_Action
  38. {
  39. /**
  40. * Landing page; forwards to listAction()
  41. */
  42. public function indexAction()
  43. {
  44. $this->_forward('list');
  45. }
  46. /**
  47. * List news items
  48. */
  49. public function listAction()
  50. {
  51. }
  52. /**
  53. * View a news item
  54. */
  55. public function viewAction()
  56. {
  57. }
  58. }
  59. ]]></programlisting>
  60. <para>
  61. Let's say that we want the <methodname>listAction()</methodname> to also be
  62. available in an <acronym>XML</acronym> format. Instead of creating a different action, we
  63. can hint that it can return an <acronym>XML</acronym> response:
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. class NewsController extends Zend_Controller_Action
  67. {
  68. public function init()
  69. {
  70. $contextSwitch = $this->_helper->getHelper('contextSwitch');
  71. $contextSwitch->addActionContext('list', 'xml')
  72. ->initContext();
  73. }
  74. // ...
  75. }
  76. ]]></programlisting>
  77. <para>
  78. What this will do is:
  79. </para>
  80. <itemizedlist>
  81. <listitem><para>
  82. Set the 'Content-Type' response header to '<filename>text/xml</filename>'.
  83. </para></listitem>
  84. <listitem><para>
  85. Change the view suffix to '<filename>xml.phtml</filename>' (or, if you use an
  86. alternate view suffix, 'xml.[your suffix]').
  87. </para></listitem>
  88. </itemizedlist>
  89. <para>
  90. Now, you'll need to create a new view script, '<filename>news/list.xml.phtml</filename>',
  91. which will create and render the <acronym>XML</acronym>.
  92. </para>
  93. <para>
  94. To determine if a request should initiate a context switch, the helper
  95. checks for a token in the request object. By default, it looks for the
  96. 'format' parameter, though this may be configured. This means that, in
  97. most cases, to trigger a context switch, you can add a 'format'
  98. parameter to your request:
  99. </para>
  100. <itemizedlist>
  101. <listitem><para>
  102. Via <acronym>URL</acronym> parameter: <filename>/news/list/format/xml</filename>
  103. (recall, the default routing schema allows for arbitrary key to value pairs
  104. following the action)
  105. </para></listitem>
  106. <listitem><para>
  107. Via <constant>GET</constant> parameter: <command>/news/list?format=xml</command>
  108. </para></listitem>
  109. </itemizedlist>
  110. <para>
  111. <emphasis>ContextSwitch</emphasis> allows you to specify arbitrary contexts,
  112. including what suffix change will occur (if any), any response headers
  113. that should be sent, and arbitrary callbacks for initialization and post
  114. processing.
  115. </para>
  116. <sect4 id="zend.controller.actionhelpers.contextswitch.contexts">
  117. <title>Default Contexts Available</title>
  118. <para>
  119. By default, two contexts are available to the
  120. <emphasis>ContextSwitch</emphasis> helper: json and xml.
  121. </para>
  122. <itemizedlist>
  123. <listitem>
  124. <para>
  125. <emphasis><acronym>JSON</acronym></emphasis>. The <acronym>JSON</acronym>
  126. context sets the 'Content-Type' response header to
  127. '<filename>application/json</filename>', and the view script suffix to
  128. '<filename>json.phtml</filename>'.
  129. </para>
  130. <para>
  131. By default, however, no view script is required. It will
  132. simply serialize all view variables, and emit the <acronym>JSON</acronym>
  133. response immediately.
  134. </para>
  135. <para>
  136. This behaviour can be disabled by turning off the automatic
  137. <acronym>JSON</acronym> serialization:
  138. </para>
  139. <programlisting language="php"><![CDATA[
  140. $this->_helper->contextSwitch()->setAutoJsonSerialization(false);
  141. ]]></programlisting>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <emphasis><acronym>XML</acronym></emphasis>. The <acronym>XML</acronym> context
  146. sets the 'Content-Type' response header to '<filename>text/xml</filename>', and
  147. the view script suffix to '<filename>xml.phtml</filename>'. You will need to
  148. create a new view script for the context.
  149. </para>
  150. </listitem>
  151. </itemizedlist>
  152. </sect4>
  153. <sect4 id="zend.controller.actionhelpers.contextswitch.custom">
  154. <title>Creating Custom Contexts</title>
  155. <para>
  156. Sometimes, the default contexts are not enough. For instance, you may wish to return
  157. <acronym>YAML</acronym>, or serialized <acronym>PHP</acronym>, an
  158. <acronym>RSS</acronym> or <acronym>ATOM</acronym> feed, etc.
  159. <emphasis>ContextSwitch</emphasis> allows you to do so.
  160. </para>
  161. <para>
  162. The easiest way to add a new context is via the
  163. <methodname>addContext()</methodname> method. This method takes two arguments,
  164. the name of the context, and an array specification. The
  165. specification should include one or more of the following:
  166. </para>
  167. <itemizedlist>
  168. <listitem>
  169. <para><emphasis>suffix</emphasis>: the suffix to prepend to the
  170. default view suffix as registered in the ViewRenderer.</para>
  171. </listitem>
  172. <listitem>
  173. <para><emphasis>headers</emphasis>: an array of header to value
  174. pairs you wish sent as part of the response.</para>
  175. </listitem>
  176. <listitem>
  177. <para><emphasis>callbacks</emphasis>: an array containing one or
  178. more of the keys 'init' or 'post', pointing to valid <acronym>PHP</acronym>
  179. callbacks that can be used for context initialization and post
  180. processing.</para>
  181. <para>Initialization callbacks occur when the context is
  182. detected by <emphasis>ContextSwitch</emphasis>. You can use it to
  183. perform arbitrary logic that should occur. As an example,
  184. the <acronym>JSON</acronym> context uses a callback to disable the ViewRenderer
  185. when the automatic <acronym>JSON</acronym> serialization is on.</para>
  186. <para>Post processing occurs during the action's
  187. <methodname>postDispatch()</methodname> routine, and can be used to perform
  188. arbitrary logic. As an example, the <acronym>JSON</acronym> context uses a callback
  189. to determine if the automatic <acronym>JSON</acronym> serialization is on; if so,
  190. it serializes the view variables to <acronym>JSON</acronym> and sends the response,
  191. but if not, it re-enables the ViewRenderer.</para>
  192. </listitem>
  193. </itemizedlist>
  194. <para>
  195. There are a variety of methods for interacting with contexts:
  196. </para>
  197. <itemizedlist>
  198. <listitem><para>
  199. <methodname>addContext($context, array $spec)</methodname>: add a new
  200. context. Throws an exception if the context already exists.
  201. </para></listitem>
  202. <listitem><para>
  203. <methodname>setContext($context, array $spec)</methodname>: add a new
  204. context or overwrite an existing context. Uses the same
  205. specification as <methodname>addContext()</methodname>.
  206. </para></listitem>
  207. <listitem><para>
  208. <methodname>addContexts(array $contexts)</methodname>: add many contexts at
  209. once. The <varname>$contexts</varname> array should be an array of
  210. context to specification pairs. If any of the contexts already
  211. exists, it will throw an exception.
  212. </para></listitem>
  213. <listitem><para>
  214. <methodname>setContexts(array $contexts)</methodname>: add new contexts and
  215. overwrite existing ones. Uses the same specification as
  216. <methodname>addContexts()</methodname>.
  217. </para></listitem>
  218. <listitem><para>
  219. <methodname>hasContext($context)</methodname>: returns <constant>TRUE</constant> if
  220. the context exists, <constant>FALSE</constant> otherwise.
  221. </para></listitem>
  222. <listitem><para> <methodname>getContext($context)</methodname>: retrieve a
  223. single context by name. Returns an array following the
  224. specification used in <methodname>addContext()</methodname>.
  225. </para></listitem>
  226. <listitem><para>
  227. <methodname>getContexts()</methodname>: retrieve all contexts. Returns an
  228. array of context to specification pairs.
  229. </para></listitem>
  230. <listitem><para>
  231. <methodname>removeContext($context)</methodname>: remove a single context by
  232. name. Returns <constant>TRUE</constant> if successful, <constant>FALSE</constant>
  233. if the context was not found.
  234. </para></listitem>
  235. <listitem><para>
  236. <methodname>clearContexts()</methodname>: remove all contexts.
  237. </para></listitem>
  238. </itemizedlist>
  239. </sect4>
  240. <sect4 id="zend.controller.actionhelpers.contextswitch.actions">
  241. <title>Setting Contexts Per Action</title>
  242. <para>
  243. There are two mechanisms for setting available contexts. You can
  244. either manually create arrays in your controller, or use several
  245. methods in <emphasis>ContextSwitch</emphasis> to assemble them.
  246. </para>
  247. <para>
  248. The principle method for adding action to context relations is
  249. <methodname>addActionContext()</methodname>. It expects two arguments, the
  250. action to which the context is being added, and either the name of a
  251. context or an array of contexts. As an example, consider the
  252. following controller class:
  253. </para>
  254. <programlisting language="php"><![CDATA[
  255. class FooController extends Zend_Controller_Action
  256. {
  257. public function listAction()
  258. {
  259. }
  260. public function viewAction()
  261. {
  262. }
  263. public function commentsAction()
  264. {
  265. }
  266. public function updateAction()
  267. {
  268. }
  269. }
  270. ]]></programlisting>
  271. <para>
  272. Let's say we wanted to add an <acronym>XML</acronym> context to the 'list' action, and
  273. <acronym>XML</acronym> and <acronym>JSON</acronym> contexts to the 'comments' action.
  274. We could do so in the <methodname>init()</methodname> method:
  275. </para>
  276. <programlisting language="php"><![CDATA[
  277. class FooController extends Zend_Controller_Action
  278. {
  279. public function init()
  280. {
  281. $this->_helper->contextSwitch()
  282. ->addActionContext('list', 'xml')
  283. ->addActionContext('comments', array('xml', 'json'))
  284. ->initContext();
  285. }
  286. }
  287. ]]></programlisting>
  288. <para>
  289. Alternately, you could simply define the array property
  290. <varname>$contexts</varname>:
  291. </para>
  292. <programlisting language="php"><![CDATA[
  293. class FooController extends Zend_Controller_Action
  294. {
  295. public $contexts = array(
  296. 'list' => array('xml'),
  297. 'comments' => array('xml', 'json')
  298. );
  299. public function init()
  300. {
  301. $this->_helper->contextSwitch()->initContext();
  302. }
  303. }
  304. ]]></programlisting>
  305. <para>
  306. The above is less overhead, but also prone to potential errors.
  307. </para>
  308. <para>
  309. The following methods can be used to build the context mappings:
  310. </para>
  311. <itemizedlist>
  312. <listitem>
  313. <para>
  314. <methodname>addActionContext($action, $context)</methodname>: marks one
  315. or more contexts as available to an action. If mappings
  316. already exists, simply appends to those mappings.
  317. <varname>$context</varname> may be a single context, or an array
  318. of contexts.
  319. </para>
  320. <para>
  321. A value of <constant>TRUE</constant> for the context will mark
  322. all available contexts as available for the action.
  323. </para>
  324. <para>
  325. An empty value for <varname>$context</varname> will disable all contexts for
  326. the given action.
  327. </para>
  328. </listitem>
  329. <listitem><para>
  330. <methodname>setActionContext($action, $context)</methodname>: marks one
  331. or more contexts as available to an action. If mappings
  332. already exists, it replaces them with those specified.
  333. <varname>$context</varname> may be a single context, or an array
  334. of contexts.
  335. </para></listitem>
  336. <listitem><para>
  337. <methodname>addActionContexts(array $contexts)</methodname>: add several
  338. action to context pairings at once. <varname>$contexts</varname>
  339. should be an associative array of action to context pairs. It
  340. proxies to <methodname>addActionContext()</methodname>, meaning that if
  341. pairings already exist, it appends to them.
  342. </para></listitem>
  343. <listitem><para>
  344. <methodname>setActionContexts(array $contexts)</methodname>: acts like
  345. <methodname>addActionContexts()</methodname>, but overwrites existing
  346. action to context pairs.
  347. </para></listitem>
  348. <listitem><para>
  349. <methodname>hasActionContext($action, $context)</methodname>: determine
  350. if a particular action has a given context.
  351. </para></listitem>
  352. <listitem><para>
  353. <methodname>getActionContexts($action = null)</methodname>: returns
  354. either all contexts for a given action, or all
  355. action to context pairs.
  356. </para></listitem>
  357. <listitem><para>
  358. <methodname>removeActionContext($action, $context)</methodname>: remove
  359. one or more contexts from a given action.
  360. <varname>$context</varname> may be a single context or an array of
  361. contexts.
  362. </para></listitem>
  363. <listitem><para>
  364. <methodname>clearActionContexts($action = null)</methodname>: remove all
  365. contexts from a given action, or from all actions with
  366. contexts.
  367. </para></listitem>
  368. </itemizedlist>
  369. </sect4>
  370. <sect4 id="zend.controller.actionhelpers.contextswitch.initcontext">
  371. <title>Initializing Context Switching</title>
  372. <para>
  373. To initialize context switching, you need to call
  374. <methodname>initContext()</methodname> in your action controller:
  375. </para>
  376. <programlisting language="php"><![CDATA[
  377. class NewsController extends Zend_Controller_Action
  378. {
  379. public function init()
  380. {
  381. $this->_helper->contextSwitch()->initContext();
  382. }
  383. }
  384. ]]></programlisting>
  385. <para>
  386. In some cases, you may want to force the context used; for instance,
  387. you may only want to allow the <acronym>XML</acronym> context if context switching is
  388. activated. You can do so by passing the context to
  389. <methodname>initContext()</methodname>:
  390. </para>
  391. <programlisting language="php"><![CDATA[
  392. $contextSwitch->initContext('xml');
  393. ]]></programlisting>
  394. </sect4>
  395. <sect4 id="zend.controller.actionhelpers.contextswitch.misc">
  396. <title>Additional Functionality</title>
  397. <para>
  398. A variety of methods can be used to alter the behaviour of the
  399. <emphasis>ContextSwitch</emphasis> helper. These include:
  400. </para>
  401. <itemizedlist>
  402. <listitem>
  403. <para>
  404. <methodname>setAutoJsonSerialization($flag)</methodname>: By default,
  405. <acronym>JSON</acronym> contexts will serialize any view variables to
  406. <acronym>JSON</acronym> notation and return this as a response. If you wish to
  407. create your own response, you should turn this off; this
  408. needs to be done prior to the call to
  409. <methodname>initContext()</methodname>.
  410. </para>
  411. <programlisting language="php"><![CDATA[
  412. $contextSwitch->setAutoJsonSerialization(false);
  413. $contextSwitch->initContext();
  414. ]]></programlisting>
  415. <para>
  416. You can retrieve the value of the flag with
  417. <methodname>getAutoJsonSerialization()</methodname>.
  418. </para>
  419. </listitem>
  420. <listitem>
  421. <para>
  422. <methodname>setSuffix($context, $suffix,
  423. $prependViewRendererSuffix)</methodname>: With this method,
  424. you can specify a different suffix to use for a given
  425. context. The third argument is used to indicate whether or
  426. not to prepend the current ViewRenderer suffix with the new
  427. suffix; this flag is enabled by default.
  428. </para>
  429. <para>
  430. Passing an empty value to the suffix will cause only the
  431. ViewRenderer suffix to be used.
  432. </para>
  433. </listitem>
  434. <listitem>
  435. <para>
  436. <methodname>addHeader($context, $header, $content)</methodname>: Add a
  437. response header for a given context. <varname>$header</varname> is
  438. the header name, and <varname>$content</varname> is the value to
  439. pass for that header.
  440. </para>
  441. <para>
  442. Each context can have multiple headers;
  443. <methodname>addHeader()</methodname> adds additional headers to the
  444. context's header stack.
  445. </para>
  446. <para>
  447. If the <varname>$header</varname> specified already exists for the
  448. context, an exception will be thrown.
  449. </para>
  450. </listitem>
  451. <listitem>
  452. <para>
  453. <methodname>setHeader($context, $header, $content)</methodname>:
  454. <methodname>setHeader()</methodname> acts just like
  455. <methodname>addHeader()</methodname>, except it allows you to overwrite
  456. existing context headers.
  457. </para>
  458. </listitem>
  459. <listitem>
  460. <para>
  461. <methodname>addHeaders($context, array $headers)</methodname>: Add
  462. multiple headers at once to a given context. Proxies to
  463. <methodname>addHeader()</methodname>, so if the header already exists,
  464. an exception will be thrown. <varname>$headers</varname> is an
  465. array of header to context pairs.
  466. </para>
  467. </listitem>
  468. <listitem>
  469. <para>
  470. <methodname>setHeaders($context, array $headers.)</methodname>: like
  471. <methodname>addHeaders()</methodname>, except it proxies to
  472. <methodname>setHeader()</methodname>, allowing you to overwrite existing
  473. headers.
  474. </para>
  475. </listitem>
  476. <listitem>
  477. <para>
  478. <methodname>getHeader($context, $header)</methodname>: retrieve the
  479. value of a header for a given context. Returns <constant>NULL</constant> if not
  480. found.
  481. </para>
  482. </listitem>
  483. <listitem>
  484. <para>
  485. <methodname>removeHeader($context, $header)</methodname>: remove a
  486. single header for a given context.
  487. </para>
  488. </listitem>
  489. <listitem>
  490. <para>
  491. <methodname>clearHeaders($context, $header)</methodname>: remove all
  492. headers for a given context.
  493. </para>
  494. </listitem>
  495. <listitem>
  496. <para>
  497. <methodname>setCallback($context, $trigger, $callback)</methodname>: set
  498. a callback at a given trigger for a given context. Triggers
  499. may be either 'init' or 'post' (indicating callback will be
  500. called at either context initialization or postDispatch).
  501. <varname>$callback</varname> should be a valid <acronym>PHP</acronym> callback.
  502. </para>
  503. </listitem>
  504. <listitem>
  505. <para>
  506. <methodname>setCallbacks($context, array $callbacks)</methodname>: set
  507. multiple callbacks for a given context. <varname>$callbacks</varname>
  508. should be trigger to callback pairs. In actuality, the most callbacks
  509. that can be registered are two, one for initialization and
  510. one for post processing.
  511. </para>
  512. </listitem>
  513. <listitem>
  514. <para>
  515. <methodname>getCallback($context, $trigger)</methodname>: retrieve a
  516. callback for a given trigger in a given context.
  517. </para>
  518. </listitem>
  519. <listitem>
  520. <para>
  521. <methodname>getCallbacks($context)</methodname>: retrieve all callbacks
  522. for a given context. Returns an array of trigger to callback
  523. pairs.
  524. </para>
  525. </listitem>
  526. <listitem>
  527. <para>
  528. <methodname>removeCallback($context, $trigger)</methodname>: remove a
  529. callback for a given trigger and context.
  530. </para>
  531. </listitem>
  532. <listitem>
  533. <para>
  534. <methodname>clearCallbacks($context)</methodname>: remove all
  535. callbacks for a given context.
  536. </para>
  537. </listitem>
  538. <listitem>
  539. <para>
  540. <methodname>setContextParam($name)</methodname>: set the request
  541. parameter to check when determining if a context switch has
  542. been requested. The value defaults to 'format', but this
  543. accessor can be used to set an alternate value.
  544. </para>
  545. <para>
  546. <methodname>getContextParam()</methodname> can be used to retrieve the
  547. current value.
  548. </para>
  549. </listitem>
  550. <listitem>
  551. <para>
  552. <methodname>setAutoDisableLayout($flag)</methodname>: By default,
  553. layouts are disabled when a context switch occurs; this is
  554. because typically layouts will only be used for returning
  555. normal responses, and have no meaning in alternate contexts.
  556. However, if you wish to use layouts (perhaps you may have a
  557. layout for the new context), you can change this behaviour
  558. by passing a <constant>FALSE</constant> value to
  559. <methodname>setAutoDisableLayout()</methodname>. You should do this
  560. <emphasis>before</emphasis> calling
  561. <methodname>initContext()</methodname>.
  562. </para>
  563. <para>
  564. To get the value of this flag, use the accessor
  565. <methodname>getAutoDisableLayout()</methodname>.
  566. </para>
  567. </listitem>
  568. <listitem>
  569. <para>
  570. <methodname>getCurrentContext()</methodname> can be used to determine
  571. what context was detected, if any. This returns <constant>NULL</constant> if no
  572. context switch occurred, or if called before
  573. <methodname>initContext()</methodname> has been invoked.
  574. </para>
  575. </listitem>
  576. </itemizedlist>
  577. </sect4>
  578. <sect4 id="zend.controller.actionhelpers.contextswitch.ajaxcontext">
  579. <title>AjaxContext Functionality</title>
  580. <para>
  581. The <emphasis>AjaxContext</emphasis> helper extends
  582. <emphasis>ContextSwitch</emphasis>, so all of the functionality listed for
  583. <emphasis>ContextSwitch</emphasis> is available to it. There are a few key
  584. differences, however.
  585. </para>
  586. <para>
  587. First, it uses a different action controller property for
  588. determining contexts, <varname>$ajaxable</varname>. This is so you can
  589. have different contexts used for <acronym>AJAX</acronym> versus normal
  590. <acronym>HTTP</acronym> requests. The various
  591. *<methodname>ActionContext()</methodname>* methods of
  592. <emphasis>AjaxContext</emphasis> will write to this property.
  593. </para>
  594. <para>
  595. Second, it will only trigger if an XmlHttpRequest has occurred, as
  596. determined by the request object's <methodname>isXmlHttpRequest()</methodname>
  597. method. Thus, if the context parameter ('format') is passed in the
  598. request, but the request was not made as an XmlHttpRequest, no
  599. context switch will trigger.
  600. </para>
  601. <para>
  602. Third, <emphasis>AjaxContext</emphasis> adds an additional context,
  603. <acronym>HTML</acronym>. In this context, it sets the suffix to
  604. '<filename>ajax.phtml</filename>' in order to differentiate the context from a normal
  605. request. No additional headers are returned.
  606. </para>
  607. <example id="zend.controller.actionhelpers.contextswitch.ajaxcontext.example">
  608. <title>Allowing Actions to Respond To Ajax Requests</title>
  609. <para>
  610. In this following example, we're allowing requests to the
  611. actions 'view', 'form', and 'process' to respond to <acronym>AJAX</acronym>
  612. requests. In the first two cases, 'view' and 'form', we'll
  613. return <acronym>HTML</acronym> snippets with which to update the page; in the
  614. latter, we'll return <acronym>JSON</acronym>.
  615. </para>
  616. <programlisting language="php"><![CDATA[
  617. class CommentController extends Zend_Controller_Action
  618. {
  619. public function init()
  620. {
  621. $ajaxContext = $this->_helper->getHelper('AjaxContext');
  622. $ajaxContext->addActionContext('view', 'html')
  623. ->addActionContext('form', 'html')
  624. ->addActionContext('process', 'json')
  625. ->initContext();
  626. }
  627. public function viewAction()
  628. {
  629. // Pull a single comment to view.
  630. // When AjaxContext detected, uses the comment/view.ajax.phtml
  631. // view script.
  632. }
  633. public function formAction()
  634. {
  635. // Render the "add new comment" form.
  636. // When AjaxContext detected, uses the comment/form.ajax.phtml
  637. // view script.
  638. }
  639. public function processAction()
  640. {
  641. // Process a new comment
  642. // Return the results as JSON; simply assign the results as
  643. // view variables, and JSON will be returned.
  644. }
  645. }
  646. ]]></programlisting>
  647. <para>
  648. On the client end, your <acronym>AJAX</acronym> library will simply request the
  649. endpoints '<filename>/comment/view</filename>',
  650. '<filename>/comment/form</filename>', and
  651. '<filename>/comment/process</filename>', and pass the 'format' parameter:
  652. '<filename>/comment/view/format/html</filename>',
  653. '<filename>/comment/form/format/html</filename>',
  654. '<filename>/comment/process/format/json</filename>'. (Or you can pass the parameter
  655. via query string: e.g., "?format=json".)
  656. </para>
  657. <para>
  658. Assuming your library passes the 'X-Requested-With:
  659. XmlHttpRequest' header, these actions will then return the
  660. appropriate response format.
  661. </para>
  662. </example>
  663. </sect4>
  664. </sect3>
  665. <!--
  666. vim:se ts=4 sw=4 et:
  667. -->