Zend_Controller-ActionHelpers-ViewRenderer.xml 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.actionhelpers.viewrenderer">
  4. <title>ViewRenderer</title>
  5. <sect4 id="zend.controller.actionhelper.viewrenderer.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <emphasis>ViewRenderer</emphasis> helper is designed to satisfy the
  9. following goals:
  10. </para>
  11. <itemizedlist>
  12. <listitem>
  13. <para>
  14. Eliminate the need to instantiate view objects within
  15. controllers; view objects will be automatically registered
  16. with the controller.
  17. </para>
  18. </listitem>
  19. <listitem>
  20. <para>
  21. Automatically set view script, helper, and filter paths
  22. based on the current module, and automatically associate
  23. the current module name as a class prefix for helper and
  24. filter classes.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. Create a globally available view object for all dispatched
  30. controllers and actions.
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. Allow the developer to set default view rendering options
  36. for all controllers.
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. Add the ability to automatically render a view script with
  42. no intervention.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. Allow the developer to create her own specifications for
  48. the view base path and for view script paths.
  49. </para>
  50. </listitem>
  51. </itemizedlist>
  52. <note>
  53. <para>
  54. If you perform a <methodname>_forward()</methodname>,
  55. <methodname>redirect()</methodname>, or
  56. <methodname>render()</methodname> manually, autorendering will not occur, as
  57. by performing any of these actions you are telling the
  58. <emphasis>ViewRenderer</emphasis> that you are determining your own
  59. output.
  60. </para>
  61. </note>
  62. <note>
  63. <para>
  64. The <emphasis>ViewRenderer</emphasis> is enabled by default. You may
  65. disable it via the front controller <emphasis>noViewRenderer</emphasis>
  66. param (<command>$front->setParam('noViewRenderer', true);</command>) or
  67. removing the helper from the helper broker stack
  68. (<methodname>Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer')</methodname>).
  69. </para>
  70. <para>
  71. If you wish to modify settings of the <emphasis>ViewRenderer</emphasis>
  72. prior to dispatching the front controller, you may do so in one
  73. of two ways:
  74. </para>
  75. <itemizedlist>
  76. <listitem>
  77. <para>
  78. Instantiate and register your own
  79. <emphasis>ViewRenderer</emphasis> object and pass it to the
  80. helper broker:
  81. </para>
  82. <programlisting language="php"><![CDATA[
  83. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  84. $viewRenderer->setView($view)
  85. ->setViewSuffix('php');
  86. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  87. ]]></programlisting>
  88. </listitem>
  89. <listitem>
  90. <para>
  91. Initialize and/or retrieve a <emphasis>ViewRenderer</emphasis>
  92. object on demand via the helper broker:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. $viewRenderer =
  96. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  97. $viewRenderer->setView($view)
  98. ->setViewSuffix('php');
  99. ]]></programlisting>
  100. </listitem>
  101. </itemizedlist>
  102. </note>
  103. </sect4>
  104. <sect4 id="zend.controller.actionhelper.viewrenderer.api">
  105. <title>API</title>
  106. <para>
  107. At its most basic usage, you simply instantiate the
  108. <emphasis>ViewRenderer</emphasis> and pass it to the action helper broker.
  109. The easiest way to instantiate it and register in one go is to use
  110. the helper broker's <methodname>getStaticHelper()</methodname> method:
  111. </para>
  112. <programlisting language="php"><![CDATA[
  113. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  114. ]]></programlisting>
  115. <para>
  116. The first time an action controller is instantiated, it will trigger
  117. the <emphasis>ViewRenderer</emphasis> to instantiate a view object. Each
  118. time a controller is instantiated, the <emphasis>ViewRenderer</emphasis>'s
  119. <methodname>init()</methodname> method is called, which will cause it to set the
  120. view property of the action controller, and call
  121. <methodname>addScriptPath()</methodname> with a path relative to the current
  122. module; this will be called with a class prefix named after the
  123. current module, effectively namespacing all helper and filter
  124. classes you define for the module.
  125. </para>
  126. <para>
  127. Each time <methodname>postDispatch()</methodname> is called, it will call
  128. <methodname>render()</methodname> for the current action.
  129. </para>
  130. <para>
  131. As an example, consider the following class:
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. // A controller class, foo module:
  135. class Foo_BarController extends Zend_Controller_Action
  136. {
  137. // Render bar/index.phtml by default; no action required
  138. public function indexAction()
  139. {
  140. }
  141. // Render bar/populate.phtml with variable 'foo' set to 'bar'.
  142. // Since view object defined at preDispatch(), it's already available.
  143. public function populateAction()
  144. {
  145. $this->view->foo = 'bar';
  146. }
  147. }
  148. ...
  149. // in one of your view scripts:
  150. $this->foo(); // call Foo_View_Helper_Foo::foo()
  151. ]]></programlisting>
  152. <para>
  153. The <emphasis>ViewRenderer</emphasis> also defines a number of accessors to
  154. allow setting and retrieving view options:
  155. </para>
  156. <itemizedlist>
  157. <listitem>
  158. <para>
  159. <methodname>setView($view)</methodname> allows you to set the view
  160. object for the <emphasis>ViewRenderer</emphasis>. It gets set as
  161. the public class property <varname>$view</varname>.
  162. </para>
  163. </listitem>
  164. <listitem>
  165. <para>
  166. <methodname>setNeverRender($flag = true)</methodname> can be used to
  167. disable or enable autorendering globally, i.e., for all
  168. controllers. If set to <constant>TRUE</constant>,
  169. <methodname>postDispatch()</methodname>
  170. will not automatically call <methodname>render()</methodname> in the
  171. current controller. <methodname>getNeverRender()</methodname> retrieves
  172. the current value.
  173. </para>
  174. </listitem>
  175. <listitem>
  176. <para>
  177. <methodname>setNoRender($flag = true)</methodname> can be used to
  178. disable or enable autorendering. If set to <constant>TRUE</constant>,
  179. <methodname>postDispatch()</methodname> will not automatically call
  180. <methodname>render()</methodname> in the current controller. This
  181. setting is reset each time <methodname>preDispatch()</methodname> is
  182. called (i.e., you need to set this flag for each controller
  183. for which you don't want autorenderering to occur).
  184. <methodname>getNoRender()</methodname> retrieves the current value.
  185. </para>
  186. </listitem>
  187. <listitem>
  188. <para>
  189. <methodname>setNoController($flag = true)</methodname> can be used to
  190. tell <methodname>render()</methodname> not to look for the action script
  191. in a subdirectory named after the controller (which is the
  192. default behaviour). <methodname>getNoController()</methodname> retrieves
  193. the current value.
  194. </para>
  195. </listitem>
  196. <listitem>
  197. <para>
  198. <methodname>setNeverController($flag = true)</methodname> is analogous
  199. to <methodname>setNoController()</methodname>, but works on a global
  200. level -- i.e., it will not be reset for each dispatched
  201. action. <methodname>getNeverController()</methodname> retrieves
  202. the current value.
  203. </para>
  204. </listitem>
  205. <listitem>
  206. <para>
  207. <methodname>setScriptAction($name)</methodname> can be used to
  208. specify the action script to render. <varname>$name</varname>
  209. should be the name of the script minus the file suffix (and
  210. without the controller subdirectory, unless
  211. <emphasis>noController</emphasis> has been turned on). If not
  212. specified, it looks for a view script named after the action
  213. in the request object. <methodname>getScriptAction()</methodname>
  214. retrieves the current value.
  215. </para>
  216. </listitem>
  217. <listitem>
  218. <para>
  219. <methodname>setResponseSegment($name)</methodname> can be used to
  220. specify which response object named segment to render into.
  221. If not specified, it renders into the default segment.
  222. <methodname>getResponseSegment()</methodname> retrieves the current
  223. value.
  224. </para>
  225. </listitem>
  226. <listitem>
  227. <para>
  228. <methodname>initView($path, $prefix, $options)</methodname> may be called
  229. to specify the base view path, class prefix for helper and
  230. filter scripts, and <emphasis>ViewRenderer</emphasis> options. You
  231. may pass any of the following flags:
  232. <emphasis>neverRender</emphasis>, <emphasis>noRender</emphasis>,
  233. <emphasis>noController</emphasis>, <emphasis>scriptAction</emphasis>, and
  234. <emphasis>responseSegment</emphasis>.
  235. </para>
  236. </listitem>
  237. <listitem>
  238. <para>
  239. <methodname>setRender($action = null, $name = null, $noController
  240. = false)</methodname> allows you to set any of
  241. <emphasis>scriptAction</emphasis>, <emphasis>responseSegment</emphasis>, and
  242. <emphasis>noController</emphasis> in one pass. <methodname>direct()</methodname>
  243. is an alias to this method, allowing you to call this method
  244. easily from your controller:
  245. </para>
  246. <programlisting language="php"><![CDATA[
  247. // Render 'foo' instead of current action script
  248. $this->_helper->viewRenderer('foo');
  249. // render form.phtml to the 'html' response segment, without using a
  250. // controller view script subdirectory:
  251. $this->_helper->viewRenderer('form', 'html', true);
  252. ]]></programlisting>
  253. <note><para>
  254. <methodname>setRender()</methodname> and <methodname>direct()</methodname>
  255. don't actually render the view script, but instead set hints
  256. that <methodname>postDispatch()</methodname> and
  257. <methodname>render()</methodname> will use to render the view.
  258. </para></note>
  259. </listitem>
  260. </itemizedlist>
  261. <para>
  262. The constructor allows you to optionally pass the view object and
  263. <emphasis>ViewRenderer</emphasis> options; it accepts the same flags as
  264. <methodname>initView()</methodname>:
  265. </para>
  266. <programlisting language="php"><![CDATA[
  267. $view = new Zend_View(array('encoding' => 'UTF-8'));
  268. $options = array('noController' => true, 'neverRender' => true);
  269. $viewRenderer =
  270. new Zend_Controller_Action_Helper_ViewRenderer($view, $options);
  271. ]]></programlisting>
  272. <para>
  273. There are several additional methods for customizing path
  274. specifications used for determining the view base path to add to the
  275. view object, and the view script path to use when autodetermining
  276. the view script to render. These methods each take one or more of
  277. the following placeholders:
  278. </para>
  279. <itemizedlist>
  280. <listitem>
  281. <para>
  282. <emphasis>:moduleDir</emphasis> refers to the current module's base
  283. directory (by convention, the parent directory of the
  284. module's controller directory).
  285. </para>
  286. </listitem>
  287. <listitem>
  288. <para>
  289. <emphasis>:module</emphasis> refers to the current module name.
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <emphasis>:controller</emphasis> refers to the current controller name.
  295. </para>
  296. </listitem>
  297. <listitem>
  298. <para>
  299. <emphasis>:action</emphasis> refers to the current action name.
  300. </para>
  301. </listitem>
  302. <listitem>
  303. <para>
  304. <emphasis>:suffix</emphasis> refers to the view script suffix (which
  305. may be set via <methodname>setViewSuffix()</methodname>).
  306. </para>
  307. </listitem>
  308. </itemizedlist>
  309. <para>
  310. The methods for controlling path specifications are:
  311. </para>
  312. <itemizedlist>
  313. <listitem>
  314. <para>
  315. <methodname>setViewBasePathSpec($spec)</methodname> allows you to change
  316. the path specification used to determine the base path to
  317. add to the view object. The default specification is
  318. <filename>:moduleDir/views</filename>. You may retrieve the current
  319. specification at any time using
  320. <methodname>getViewBasePathSpec()</methodname>.
  321. </para>
  322. </listitem>
  323. <listitem>
  324. <para>
  325. <methodname>setViewScriptPathSpec($spec)</methodname> allows you to
  326. change the path specification used to determine the path to
  327. an individual view script (minus the base view script path).
  328. The default specification is
  329. <filename>:controller/:action.:suffix</filename>. You may retrieve
  330. the current specification at any time using
  331. <methodname>getViewScriptPathSpec()</methodname>.
  332. </para>
  333. </listitem>
  334. <listitem>
  335. <para>
  336. <methodname>setViewScriptPathNoControllerSpec($spec)</methodname> allows
  337. you to change the path specification used to determine the
  338. path to an individual view script when
  339. <emphasis>noController</emphasis> is in effect (minus the base view
  340. script path). The default specification is
  341. <filename>:action.:suffix</filename>. You may retrieve the current
  342. specification at any time using
  343. <methodname>getViewScriptPathNoControllerSpec()</methodname>.
  344. </para>
  345. </listitem>
  346. </itemizedlist>
  347. <para>
  348. For fine-grained control over path specifications, you may use
  349. <link linkend="zend.filter.inflector">Zend_Filter_Inflector</link>.
  350. Under the hood, the <emphasis>ViewRenderer</emphasis> uses an inflector to
  351. perform path mappings already. To interact with the inflector --
  352. either to set your own for use, or to modify the default inflector,
  353. the following methods may be used:
  354. </para>
  355. <itemizedlist>
  356. <listitem>
  357. <para>
  358. <methodname>getInflector()</methodname> will retrieve the inflector. If
  359. none exists yet in the <emphasis>ViewRenderer</emphasis>, it creates
  360. one using the default rules.
  361. </para>
  362. <para>
  363. By default, it uses static rule references for the suffix
  364. and module directory, as well as a static target; this
  365. allows various <emphasis>ViewRenderer</emphasis> properties the
  366. ability to dynamically modify the inflector.
  367. </para>
  368. </listitem>
  369. <listitem><para>
  370. <methodname>setInflector($inflector, $reference)</methodname> allows you
  371. to set a custom inflector for use with the
  372. <emphasis>ViewRenderer</emphasis>. If <varname>$reference</varname> is
  373. <constant>TRUE</constant>, it will set the suffix and module directory as static
  374. references to <emphasis>ViewRenderer</emphasis> properties, as well
  375. as the target.
  376. </para></listitem>
  377. </itemizedlist>
  378. <note>
  379. <title>Default Lookup Conventions</title>
  380. <para>
  381. The <emphasis>ViewRenderer</emphasis> does some path normalization to
  382. make view script lookups easier. The default rules are as
  383. follows:
  384. </para>
  385. <itemizedlist>
  386. <listitem>
  387. <para>
  388. <emphasis>:module</emphasis>: MixedCase and camelCasedWords are separated by
  389. dashes, and the entire string cast to lowercase. E.g.:
  390. "FooBarBaz" becomes "foo-bar-baz".
  391. </para>
  392. <para>
  393. Internally, the inflector uses the filters
  394. <classname>Zend_Filter_Word_CamelCaseToDash</classname> and
  395. <classname>Zend_Filter_StringToLower</classname>.
  396. </para>
  397. </listitem>
  398. <listitem>
  399. <para>
  400. <emphasis>:controller</emphasis>: MixedCase and camelCasedWords are
  401. separated by dashes; underscores are converted to directory
  402. separators, and the entire string cast to lower case.
  403. Examples: "<classname>FooBar</classname>" becomes "foo-bar";
  404. "<classname>FooBar_Admin</classname>" becomes
  405. "<filename>foo-bar/admin</filename>".
  406. </para>
  407. <para>
  408. Internally, the inflector uses the filters
  409. <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
  410. <classname>Zend_Filter_Word_UnderscoreToSeparator</classname>, and
  411. <classname>Zend_Filter_StringToLower</classname>.
  412. </para>
  413. </listitem>
  414. <listitem>
  415. <para>
  416. <emphasis>:action</emphasis>: MixedCase and camelCasedWords are separated
  417. by dashes; non-alphanumeric characters are translated to
  418. dashes, and the entire string cast to lower case.
  419. Examples: "fooBar" becomes "foo-bar"; "foo-barBaz"
  420. becomes "foo-bar-baz".
  421. </para>
  422. <para>
  423. Internally, the inflector uses the filters
  424. <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
  425. <classname>Zend_Filter_PregReplace</classname>, and
  426. <classname>Zend_Filter_StringToLower</classname>.
  427. </para>
  428. </listitem>
  429. </itemizedlist>
  430. </note>
  431. <para>
  432. The final items in the <emphasis>ViewRenderer</emphasis> <acronym>API</acronym> are the
  433. methods for actually determining view script paths and rendering views.
  434. These include:
  435. </para>
  436. <itemizedlist>
  437. <listitem>
  438. <para>
  439. <methodname>renderScript($script, $name)</methodname> allows you to
  440. render a script with a path you specify, optionally to a
  441. named path segment. When using this method, the
  442. <emphasis>ViewRenderer</emphasis> does no autodetermination of the
  443. script name, but instead directly passes the
  444. <varname>$script</varname> argument directly to the view object's
  445. <methodname>render()</methodname> method.
  446. </para>
  447. <note><para>
  448. Once the view has been rendered to the response object, it
  449. sets the <emphasis>noRender</emphasis> to prevent accidentally
  450. rendering the same view script multiple times.
  451. </para></note>
  452. <note>
  453. <para>
  454. By default,
  455. <methodname>Zend_Controller_Action::renderScript()</methodname>
  456. proxies to the <emphasis>ViewRenderer</emphasis>'s
  457. <methodname>renderScript()</methodname> method.
  458. </para>
  459. </note>
  460. </listitem>
  461. <listitem>
  462. <para>
  463. <methodname>getViewScript($action, $vars)</methodname> creates the path
  464. to a view script based on the action passed and/or any
  465. variables passed in <varname>$vars</varname>. Keys for this array
  466. may include any of the path specification keys ('moduleDir',
  467. 'module', 'controller', 'action', and 'suffix'). Any
  468. variables passed will be used; otherwise, values based on
  469. the current request will be utlized.
  470. </para>
  471. <para>
  472. <methodname>getViewScript()</methodname> will use either the
  473. <emphasis>viewScriptPathSpec</emphasis> or
  474. <emphasis>viewScriptPathNoControllerSpec</emphasis> based on the
  475. setting of the <emphasis>noController</emphasis> flag.
  476. </para>
  477. <para>
  478. Word delimiters occurring in module, controller, or action names will be
  479. replaced with dashes ('-'). Thus, if you have the controller name
  480. '<command>foo.bar</command>' and the action '<command>baz:bat</command>', using
  481. the default path specification will result in a view script path of
  482. '<filename>foo-bar/baz-bat.phtml</filename>'.
  483. </para>
  484. <note>
  485. <para>
  486. By default,
  487. <methodname>Zend_Controller_Action::getViewScript()</methodname>
  488. proxies to the <emphasis>ViewRenderer</emphasis>'s
  489. <methodname>getViewScript()</methodname> method.
  490. </para>
  491. </note>
  492. </listitem>
  493. <listitem>
  494. <para>
  495. <methodname>render($action, $name, $noController)</methodname> checks
  496. first to see if either <varname>$name</varname> or
  497. <varname>$noController</varname> have been passed, and if so, sets
  498. the appropriate flags (responseSegment and noController,
  499. respectively) in the ViewRenderer. It then passes the
  500. <varname>$action</varname> argument, if any, on to
  501. <methodname>getViewScript()</methodname>. Finally, it passes the
  502. calculated view script path to <methodname>renderScript()</methodname>.
  503. </para>
  504. <note>
  505. <para>
  506. Be aware of the side-effects of using <methodname>render()</methodname>: the
  507. values you pass for the response segment name and for
  508. the noController flag will persist in the object.
  509. Additionally, noRender will be set after rendering is
  510. completed.
  511. </para>
  512. </note>
  513. <note>
  514. <para>
  515. By default,
  516. <methodname>Zend_Controller_Action::render()</methodname> proxies to
  517. the <emphasis>ViewRenderer</emphasis>'s <methodname>render()</methodname>
  518. method.
  519. </para>
  520. </note>
  521. </listitem>
  522. <listitem>
  523. <para>
  524. <methodname>renderBySpec($action, $vars, $name)</methodname> allows you
  525. to pass path specification variables in order to determine
  526. the view script path to create. It passes
  527. <varname>$action</varname> and <varname>$vars</varname> to
  528. <methodname>getScriptPath()</methodname>, and then passes the resulting
  529. script path and <varname>$name</varname> on to
  530. <methodname>renderScript()</methodname>.
  531. </para>
  532. </listitem>
  533. </itemizedlist>
  534. </sect4>
  535. <sect4 id="zend.controller.actionhelper.viewrenderer.basicusage">
  536. <title>Basic Usage Examples</title>
  537. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-1">
  538. <title>Basic Usage</title>
  539. <para>
  540. At its most basic, you simply initialize and register the
  541. <emphasis>ViewRenderer</emphasis> helper with the helper broker in your
  542. bootstrap, and then set variables in your action methods.
  543. </para>
  544. <programlisting language="php"><![CDATA[
  545. // In your bootstrap:
  546. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  547. ...
  548. // 'foo' module, 'bar' controller:
  549. class Foo_BarController extends Zend_Controller_Action
  550. {
  551. // Render bar/index.phtml by default; no action required
  552. public function indexAction()
  553. {
  554. }
  555. // Render bar/populate.phtml with variable 'foo' set to 'bar'.
  556. // Since view object defined at preDispatch(), it's already available.
  557. public function populateAction()
  558. {
  559. $this->view->foo = 'bar';
  560. }
  561. // Renders nothing as it forwards to another action; the new action
  562. // will perform any rendering
  563. public function bazAction()
  564. {
  565. $this->_forward('index');
  566. }
  567. // Renders nothing as it redirects to another location
  568. public function batAction()
  569. {
  570. $this->_redirect('/index');
  571. }
  572. }
  573. ]]></programlisting>
  574. </example>
  575. <note>
  576. <title>Naming Conventions: Word Delimiters in Controller and Action Names</title>
  577. <para>
  578. If your controller or action name is composed of several
  579. words, the dispatcher requires that these are separated on
  580. the <acronym>URL</acronym> by specific path and word delimiter characters. The
  581. <emphasis>ViewRenderer</emphasis> replaces any path delimiter found
  582. in the controller name with an actual path delimiter ('/'),
  583. and any word delimiter found with a dash ('-') when creating
  584. paths. Thus, a call to the action
  585. <filename>/foo.bar/baz.bat</filename> would dispatch to
  586. <methodname>FooBarController::bazBatAction()</methodname> in
  587. <filename>FooBarController.php</filename>, which would render
  588. <filename>foo-bar/baz-bat.phtml</filename>; a call to the action
  589. <filename>/bar_baz/baz-bat</filename> would dispatch to
  590. <methodname>Bar_BazController::bazBatAction()</methodname> in
  591. <filename>Bar/BazController.php</filename> (note the path
  592. separation) and render <filename>bar/baz/baz-bat.phtml</filename>.
  593. </para>
  594. <para>
  595. Note that the in the second example, the module is still the
  596. default module, but that, because of the existence of a path
  597. separator, the controller receives the name
  598. <classname>Bar_BazController</classname>, in
  599. <filename>Bar/BazController.php</filename>. The ViewRenderer mimics
  600. the controller directory hierarchy.
  601. </para>
  602. </note>
  603. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-2">
  604. <title>Disabling Autorender</title>
  605. <para>
  606. For some actions or controllers, you may want to turn off the
  607. autorendering -- for instance, if you're wanting to emit a
  608. different type of output (<acronym>XML</acronym>, <acronym>JSON</acronym>, etc),
  609. or if you simply want
  610. to emit nothing. You have two options: turn off all cases of
  611. autorendering (<methodname>setNeverRender()</methodname>), or simply turn it
  612. off for the current action (<methodname>setNoRender()</methodname>).
  613. </para>
  614. <programlisting language="php"><![CDATA[
  615. // Baz controller class, bar module:
  616. class Bar_BazController extends Zend_Controller_Action
  617. {
  618. public function fooAction()
  619. {
  620. // Don't auto render this action
  621. $this->_helper->viewRenderer->setNoRender();
  622. }
  623. }
  624. // Bat controller class, bar module:
  625. class Bar_BatController extends Zend_Controller_Action
  626. {
  627. public function preDispatch()
  628. {
  629. // Never auto render this controller's actions
  630. $this->_helper->viewRenderer->setNoRender();
  631. }
  632. }
  633. ]]></programlisting>
  634. </example>
  635. <note>
  636. <para>
  637. In most cases, it makes no sense to turn off autorendering
  638. globally (ala <methodname>setNeverRender()</methodname>), as the only thing
  639. you then gain from <emphasis>ViewRenderer</emphasis> is the autosetup of
  640. the view object.
  641. </para>
  642. </note>
  643. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-3">
  644. <title>Choosing a Different View Script</title>
  645. <para>
  646. Some situations require that you render a different script than
  647. one named after the action. For instance, if you have a
  648. controller that has both add and edit actions, they may both
  649. display the same 'form' view, albeit with different values set.
  650. You can easily change the script name used with either
  651. <methodname>setScriptAction()</methodname>, <methodname>setRender()</methodname>,
  652. or calling the helper as a method, which will invoke
  653. <methodname>setRender()</methodname>.
  654. </para>
  655. <programlisting language="php"><![CDATA[
  656. // Bar controller class, foo module:
  657. class Foo_BarController extends Zend_Controller_Action
  658. {
  659. public function addAction()
  660. {
  661. // Render 'bar/form.phtml' instead of 'bar/add.phtml'
  662. $this->_helper->viewRenderer('form');
  663. }
  664. public function editAction()
  665. {
  666. // Render 'bar/form.phtml' instead of 'bar/edit.phtml'
  667. $this->_helper->viewRenderer->setScriptAction('form');
  668. }
  669. public function processAction()
  670. {
  671. // do some validation...
  672. if (!$valid) {
  673. // Render 'bar/form.phtml' instead of 'bar/process.phtml'
  674. $this->_helper->viewRenderer->setRender('form');
  675. return;
  676. }
  677. // otherwise continue processing...
  678. }
  679. }
  680. ]]></programlisting>
  681. </example>
  682. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-4">
  683. <title>Modifying the Registered View</title>
  684. <para>
  685. What if you need to modify the view object -- for instance,
  686. change the helper paths, or the encoding? You can do so either
  687. by modifying the view object set in your controller, or by
  688. grabbing the view object out of the <emphasis>ViewRenderer</emphasis>;
  689. both are references to the same object.
  690. </para>
  691. <programlisting language="php"><![CDATA[
  692. // Bar controller class, foo module:
  693. class Foo_BarController extends Zend_Controller_Action
  694. {
  695. public function preDispatch()
  696. {
  697. // change view encoding
  698. $this->view->setEncoding('UTF-8');
  699. }
  700. public function bazAction()
  701. {
  702. // Get view object and set escape callback to 'htmlspecialchars'
  703. $view = $this->_helper->viewRenderer->view;
  704. $view->setEscape('htmlspecialchars');
  705. }
  706. }
  707. ]]></programlisting>
  708. </example>
  709. </sect4>
  710. <sect4 id="zend.controller.actionhelper.viewrenderer.advancedusage">
  711. <title>Advanced Usage Examples</title>
  712. <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-1">
  713. <title>Changing the Path Specifications</title>
  714. <para>
  715. In some circumstances, you may decide that the default path
  716. specifications do not fit your site's needs. For instance, you
  717. may want to have a single template tree to which you may then
  718. give access to your designers (this is very typical when using
  719. <ulink url="http://smarty.php.net/">Smarty</ulink>, for
  720. instance). In such a case, you may want to hardcode the view
  721. base path specification, and create an alternate specification
  722. for the action view script paths themselves.
  723. </para>
  724. <para>
  725. For purposes of this example, let's assume that the base path to
  726. views should be '<filename>/opt/vendor/templates</filename>', and that you wish for
  727. view scripts to be referenced by
  728. '<filename>:moduleDir/:controller/:action.:suffix</filename>'; if the
  729. <emphasis>noController</emphasis>
  730. flag has been set, you want to render out of the top level
  731. instead of in a subdirectory ('<filename>:action.:suffix</filename>'). Finally, you
  732. want to use 'tpl' as the view script filename suffix.
  733. </para>
  734. <programlisting language="php"><![CDATA[
  735. /**
  736. * In your bootstrap:
  737. */
  738. // Different view implementation
  739. $view = new ZF_Smarty();
  740. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
  741. $viewRenderer->setViewBasePathSpec('/opt/vendor/templates')
  742. ->setViewScriptPathSpec(':module/:controller/:action.:suffix')
  743. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  744. ->setViewSuffix('tpl');
  745. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  746. ]]></programlisting>
  747. </example>
  748. <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-2">
  749. <title>Rendering Multiple View Scripts from a Single Action</title>
  750. <para>
  751. At times, you may need to render multiple view scripts from a
  752. single action. This is very straightforward -- simply make
  753. multiple calls to <methodname>render()</methodname>:
  754. </para>
  755. <programlisting language="php"><![CDATA[
  756. class SearchController extends Zend_Controller_Action
  757. {
  758. public function resultsAction()
  759. {
  760. // Assume $this->model is the current model
  761. $this->view->results =
  762. $this->model->find($this->_getParam('query', '');
  763. // render() by default proxies to the ViewRenderer
  764. // Render first the search form and then the results
  765. $this->render('form');
  766. $this->render('results');
  767. }
  768. public function formAction()
  769. {
  770. // do nothing; ViewRenderer autorenders the view script
  771. }
  772. }
  773. ]]></programlisting>
  774. </example>
  775. </sect4>
  776. </sect3>
  777. <!--
  778. vim:se ts=4 sw=4 et:
  779. -->