Zend_Controller-ActionController.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <sect1 id="zend.controller.action">
  2. <title>动作控制器</title>
  3. <sect2 id="zend.controller.action.introduction">
  4. <title>简介</title>
  5. <para>
  6. <code>Zend_Controller_Action</code>是一个抽象类,当基于模型-视图-控制器(MVC)模式建立网站的时候,你可以用它来为和前端控制器使用一起来实现动作控制器。
  7. </para>
  8. <para>
  9. 为使用<code>Zend_Controller_Action</code>,你需要在实际的控制器类中把它子类化(或者为动作控制器创建你自己的基本类而使它子类化)。最基本的操作是子类化和创建对应于不同动作的动作方法,这些动作是你希望控制器来处理你的站点的动作。Zend_Controller的路由和派遣处理将在你的类里自动发现任何以'Action'结尾的方法作为潜在的控制器动作。
  10. </para>
  11. <para>
  12. 例如,你的类如下定义:
  13. </para>
  14. <programlisting role="php"><![CDATA[
  15. class FooController extends Zend_Controller_Action
  16. {
  17. public function barAction()
  18. {
  19. // do something
  20. }
  21. public function bazAction()
  22. {
  23. // do something
  24. }
  25. }
  26. ]]>
  27. </programlisting>
  28. <para>
  29. 上述 <code>FooController</code> 类(控制器<code>foo</code>)定义了两个动作:<code>bar</code>和<code>baz</code>。
  30. </para>
  31. <para>
  32. 还有更多的可以被实现,例如定制初始化动作,如果没有动作(或者有个无效动作)被指定,缺省的动作被调用,派遣之前和之后的钩子,以及无数的助手方法。这章是动作控制器功能的一个总览。
  33. </para>
  34. <note>
  35. <title>缺省行为</title>
  36. <para>
  37. 缺省地,<link linkend="zend.controller.front">前端控制器</link>激活了<link linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>动作助手。这个助手负责把视图对象注入到控制器,同时解析(rendering)视图。通过下面方法之一,可以在动作控制器里禁止它:
  38. </para>
  39. <programlisting role="php"><![CDATA[
  40. class FooController extends Zend_Controller_Action
  41. {
  42. public function init()
  43. {
  44. // 只是局部控制器;当初始化加载时,对这个控制器的所有动作有效:
  45. $this->_helper->viewRenderer->setNoRender(true);
  46. // 全局:
  47. $this->_helper->removeHelper('viewRenderer');
  48. // 也是全局,但需要和本地版本协作,以便繁殖这个控制器:
  49. Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
  50. }
  51. }
  52. ]]>
  53. </programlisting>
  54. <para>
  55. <code>initView()</code>、 <code>getViewScript()</code>、<code>render()</code>和<code>renderScript()</code> 都代理 <code>ViewRenderer</code>,除非助手不在助手经纪人(broker)里或<code>noViewRenderer</code>标志被设置。
  56. </para>
  57. <para>
  58. 通过设置<code>ViewRenderer</code>的<code>noRender</code>标记,可以简单地为一个独立的视图禁止解析(rendering):
  59. </para>
  60. <programlisting role="php"><![CDATA[
  61. class FooController extends Zend_Controller_Action
  62. {
  63. public function barAction()
  64. {
  65. // disable autorendering for this action only:
  66. $this->_helper->viewRenderer->setNoRender();
  67. }
  68. }
  69. ]]>
  70. </programlisting>
  71. <para>
  72. 禁止<code>ViewRenderer</code>的主要原因是如果你不需要视图对象或者如果你不通过视图脚本(例如,当使用动作控制器来司服网站服务协议如SOAP,XML-RPC或REST)来解析。大多数情况下,你不需要全局地禁止<code>ViewRenderer</code>,只选择性地在个别控制器或动作里禁止它。
  73. </para>
  74. </note>
  75. </sect2>
  76. <sect2 id="zend.controller.action.initialization">
  77. <title>对象初始化</title>
  78. <para>
  79. 虽然你可以总重写动作控制器的构造函数,我们不建议这么做。Zend_Controller_Action::__construct()执行一些重要的任务,如注册请求和响应对象,还有任何从前端控制器传来的invocation参数。如果你必须重写构造函数,别忘记调用<code>parent::__construct($request, $response, $invokeArgs)</code>。
  80. </para>
  81. <para>
  82. 更合适的方法来定制实例化是使用<code>init()</code>方法,它是在<code>__construct()</code>里的最后一个调用任务。例如,如果你想在实例化时连接数据库:
  83. </para>
  84. <programlisting role="php"><![CDATA[
  85. class FooController extends Zend_Controller_Action
  86. {
  87. public function init()
  88. {
  89. $this->db = Zend_Db::factory('Pdo_Mysql', array(
  90. 'host' => 'myhost',
  91. 'username' => 'user',
  92. 'password' => 'XXXXXXX',
  93. 'dbname' => 'website'
  94. ));
  95. }
  96. }
  97. ]]>
  98. </programlisting>
  99. </sect2>
  100. <sect2 id="zend.controller.action.prepostdispatch">
  101. <title>派遣前后的钩子</title>
  102. <para>
  103. <code>Zend_Controller_Action</code>指定两个方法,<code>preDispatch()</code> 和<code>postDispatch()</code>,可能被调用来bookend一个请求的动作。这在很多场合都有用:例如在运行一个动作(通过调用<code>preDispatch()</code>里的<code>_forward()</code>,动作将被跳过)前校验认证和ACLs,或者在网站范围的模板里(<code>postDispatch()</code>)替换生成的内容。
  104. </para>
  105. </sect2>
  106. <sect2 id="zend.controller.action.accessors">
  107. <title>访问器</title>
  108. <para>
  109. 无数的对象和变量与对象一起注册,并且每个都有访问器方法。
  110. </para>
  111. <itemizedlist>
  112. <listitem><para>
  113. <emphasis>请求对象</emphasis>:<code>getRequest()</code>可用来读取调用动作请求对象。
  114. </para></listitem>
  115. <listitem>
  116. <para>
  117. <emphasis>响应对象</emphasis>: <code>getResponse()</code>可用来读取收集最终响应的响应对象。一些典型的调用看起来象这样:
  118. </para>
  119. <programlisting role="php"><![CDATA[
  120. $this->getResponse()->setHeader('Content-Type', 'text/xml');
  121. $this->getResponse()->appendBody($content);
  122. ]]>
  123. </programlisting>
  124. </listitem>
  125. <listitem>
  126. <para>
  127. <emphasis>调用参数</emphasis>:前端控制器可能把参数传给路由器、派遣器和动作控制器。为了读取这些参数,可使用<code>getInvokeArg($key)</code>;另外,用<code>getInvokeArgs()</code>读取整个参数列表。
  128. </para>
  129. </listitem>
  130. <listitem>
  131. <para>
  132. <emphasis>请求参数</emphasis>:请求对象手机请求参数,如任何_GET 或 _POST 参数,或者指定在URL的路径信息里的用户参数。为了读取这些参数,可使用<code>_getParam($key)</code>或<code>_getAllParams()</code>。也可以用<code>_setParam()</code>来设置请求参数;当转发到另外的动作时这很有用。
  133. </para>
  134. <para>
  135. 用<code>_hasParam($key)</code>来测试是否一个参数存在(对逻辑分支有用)。
  136. </para>
  137. <note>
  138. <para>
  139. <code>_getParam()</code>可带有一个可选的第二个参数,如果它不是空的,就包含一个缺省的值。用它在读取值之前来消除对<code>_hasParam()</code> 的调用:
  140. </para>
  141. <programlisting role="php"><![CDATA[
  142. // Use default value of 1 if id is not set
  143. $id = $this->_getParam('id', 1);
  144. // Instead of:
  145. if ($this->_hasParam('id') {
  146. $id = $this->_getParam('id');
  147. } else {
  148. $id = 1;
  149. }
  150. ]]>
  151. </programlisting>
  152. </note>
  153. </listitem>
  154. </itemizedlist>
  155. </sect2>
  156. <sect2 id="zend.controller.action.viewintegration">
  157. <title>视图集成</title>
  158. <para>
  159. <code>Zend_Controller_Action</code>为视图继承提供了一个初步的灵活的机制。有两个方法来完成这个:<code>initView()</code> 和 <code>render()</code>;前者松散地加载<code>$view</code> public 属性,后者基于当前请求的动作来解析视图,它们使用目录层次来决定脚本路径。
  160. </para>
  161. <sect3 id="zend.controller.action.viewintegration.initview">
  162. <title>视图初始化</title>
  163. <para>
  164. <code>initView()</code>初始化视图对象。为了读取视图对象,<code>render()</code>调用<code>initView()</code>,但它可以在任何时候被初始化;缺省地,它用<code>Zend_View</code>对象来组装<code>$view</code>属性,但任何实现<code>Zend_View_Interface</code>的类可以使用。如果<code>$view</code>已经被初始化,它就简单地返回属性。
  165. </para>
  166. <para>
  167. 缺省的实现使用下面假设的目录结构:
  168. </para>
  169. <programlisting role="php"><![CDATA[
  170. applicationOrModule/
  171. controllers/
  172. IndexController.php
  173. views/
  174. scripts/
  175. index/
  176. index.phtml
  177. helpers/
  178. filters/
  179. ]]>
  180. </programlisting>
  181. <para>
  182. 换句话说,视图脚本假定放在<code>views/scripts/</code>子目录,同时假定<code> views</code>子目录还包含兄弟功能(助手和过滤器)。确定视图脚本名称和路径时,先以<code> views/scripts/</code>作为基路径,然后加上以视图脚本对应控制器命名的目录。
  183. </para>
  184. </sect3>
  185. <sect3 id="zend.controller.action.viewintegration.render">
  186. <title>解析(Rendering)视图</title>
  187. <para>
  188. <code>render()</code> 有下列特征:has the following signature:
  189. </para>
  190. <programlisting role="php"><![CDATA[
  191. string render(string $action = null,
  192. string $name = null,
  193. bool $noController = false);
  194. ]]>
  195. </programlisting>
  196. <para>
  197. <code>render()</code>解析视图脚本。如果没有传递参数,它假定请求的脚本是<code>[controller]/[action].phtml</code> (<code>.phtml</code>是<code>$viewSuffix</code>属性的值)。为<code>$action</code>传递一个值将解析在<code>[controller]</code>子目录中的模板。为用<code>[controller]</code>重写,传递一个true值给<code>$noController</code>。最后,模板被解析到响应对象;如果你希望解析到一个在响应对象里指定的<link linkend="zend.controller.response.namedsegments">named segment</link>,传递一个值给<code>$name</code>。
  198. </para>
  199. <note><para>
  200. 因为控制器和动作名字里可能包含分隔符如'_'、 '.' 和 '-',当决定视图名字时,render()把它们规格化成 '-'.在内部,它使用派遣器的字和路径分隔符来做规格化。这样,对<code>/foo.bar/baz-bat</code>的请求将解析脚本<code>foo-bar/baz-bat.phtml</code>。如果动作方法包含camelCasing,记住当决定视图脚本文件名的时候,这将变成由'-'分隔的字。
  201. </para></note>
  202. <para>
  203. 一些例子:
  204. </para>
  205. <programlisting role="php"><![CDATA[
  206. class MyController extends Zend_Controller_Action
  207. {
  208. public function fooAction()
  209. {
  210. // Renders my/foo.phtml
  211. $this->render();
  212. // Renders my/bar.phtml
  213. $this->render('bar');
  214. // Renders baz.phtml
  215. $this->render('baz', null, true);
  216. // Renders my/login.phtml to the 'form' segment of the
  217. // response object
  218. $this->render('login', 'form');
  219. // Renders site.phtml to the 'page' segment of the response
  220. // object; does not use the 'my/' subirectory
  221. $this->render('site', 'page', true);
  222. }
  223. public function bazBatAction()
  224. {
  225. // Renders my/baz-bat.phtml
  226. $this->render();
  227. }
  228. }
  229. ]]>
  230. </programlisting>
  231. </sect3>
  232. </sect2>
  233. <sect2 id="zend.controller.action.utilmethods">
  234. <title>实用方法</title>
  235. <para>
  236. 除了访问器和视图继承方法,在动作方法内部里,<code>Zend_Controller_Action</code>有若干实用方法来执行普通的任务(或在派遣的前后)。
  237. </para>
  238. <itemizedlist>
  239. <listitem>
  240. <para>
  241. <code>_forward($action, $controller = null, $module = null, array $params = null)</code> :执行另外一个动作。如果在<code>preDispatch()</code>里调用,当前请求的动作将被跳过来支持新的动作。否则,在当前动作被处理之后,在_forward()请求的动作将被执行。
  242. </para>
  243. </listitem>
  244. <listitem>
  245. <para>
  246. <code>_redirect($url, array $options = array())</code>:重定向到另外一个地方。这个方法用URL和一组可选的选项。缺省地,它执行HTTP 302 重定向。
  247. </para>
  248. <para>
  249. 选项可包括一个或多个下面这些:
  250. </para>
  251. <itemizedlist>
  252. <listitem>
  253. <para>
  254. <emphasis>exit:</emphasis>是否立即退出。如果被请求,它将干净地关闭任何打开的会话和执行重定向。
  255. </para>
  256. <para>
  257. 可以用<code>setRedirectExit()</code>访问器在控制器里全局地设置这个选项。
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <emphasis>prependBase:</emphasis>是否预先考虑基础URL和URL提供的请求对象一起注册。
  263. </para>
  264. <para>
  265. 使用<code>setRedirectPrependBase()</code>访问器,在控制器里全局地设置这个选项。
  266. </para>
  267. </listitem>
  268. <listitem>
  269. <para>
  270. <emphasis>code:</emphasis>在重定向时要用什么HTTP代码。缺省使用302;可以用从301到306之间的任何代码。
  271. </para>
  272. <para>
  273. 使用<code>setRedirectCode()</code>访问器,在控制器里全局地设置这个选项。
  274. </para>
  275. </listitem>
  276. </itemizedlist>
  277. </listitem>
  278. </itemizedlist>
  279. </sect2>
  280. <sect2 id="zend.controller.action.subclassing">
  281. <title>继承(Subclassing)动作控制器</title>
  282. <para>
  283. 为了创建动作控制器,设计上,<code>Zend_Controller_Action</code> 必须被继承。至少,需要定义控制器可能调用的动作方法。
  284. </para>
  285. <para>
  286. 除了为web应用程序创建有用的函数外,你可能发现在不同的控制器里重复同样的设置和实用方法;如果这样,创建一个继承(extends)<code>Zend_Controller_Action</code> 的基础类可能会解决问题。
  287. </para>
  288. <example id="zend.controller.action.subclassing.example-call">
  289. <title>如何处理不存在的动作</title>
  290. <para>
  291. 如果控制器的请求包括一个未定义的动作方法,<code>Zend_Controller_Action::__call()</code>将被调用。<code>__call()</code>当然是PHP中用来重载方法的魔术方法。
  292. </para>
  293. <para>
  294. 缺省地,这个方法抛出一个<code>Zend_Controller_Action_Exception</code> 来表明在控制器里没有发现要求的方法。如果要求的方法以'Action'结尾,就假设一个动作被请求并且不存在;这样的错误导致带有代码为 404 的异常。所有其它方法导致带有代码为 500 的异常。这使你很容易地在错误句柄里区分是页面没有发现还是程序错误。
  295. </para>
  296. <para>
  297. 如果想执行其它操作,你应该重写这个函数。例如,如果你想显示错误信息,可以象下面这样来写:
  298. </para>
  299. <programlisting role="php"><![CDATA[
  300. class MyController extends Zend_Controller_Action
  301. {
  302. public function __call($method, $args)
  303. {
  304. if ('Action' == substr($method, -6)) {
  305. // If the action method was not found, render the error
  306. // template
  307. return $this->render('error');
  308. }
  309. // all other methods throw an exception
  310. throw new Exception('Invalid method "'
  311. . $method
  312. . '" called',
  313. 500);
  314. }
  315. }
  316. ]]>
  317. </programlisting>
  318. <para>
  319. 另外的可能性就是你可能想转发到缺省控制页面:
  320. </para>
  321. <programlisting role="php"><![CDATA[
  322. class MyController extends Zend_Controller_Action
  323. {
  324. public function indexAction()
  325. {
  326. $this->render();
  327. }
  328. public function __call($method, $args)
  329. {
  330. if ('Action' == substr($method, -6)) {
  331. // If the action method was not found, forward to the
  332. // index action
  333. return $this->_forward('index');
  334. }
  335. // all other methods throw an exception
  336. throw new Exception('Invalid method "'
  337. . $method
  338. . '" called',
  339. 500);
  340. }
  341. }
  342. ]]>
  343. </programlisting>
  344. </example>
  345. <para>
  346. 为了定制控制器,除了重写<code>__call()</code>以外,本章前面说涉及的初始化、实用程序、访问器、视图和派遣钩子等方法都可以被重写。作为例子,如果把视图对象保存到注册表里,你可能想用象下面的代码来修改<code>initView()</code>:
  347. </para>
  348. <programlisting role="php"><![CDATA[
  349. abstract class My_Base_Controller extends Zend_Controller_Action
  350. {
  351. public function initView()
  352. {
  353. if (null === $this->view) {
  354. if (Zend_Registry::isRegistered('view')) {
  355. $this->view = Zend_Registry::get('view');
  356. } else {
  357. $this->view = new Zend_View();
  358. $this->view->setBasePath(dirname(__FILE__) . '/../views');
  359. }
  360. }
  361. return $this->view;
  362. }
  363. }
  364. ]]>
  365. </programlisting>
  366. <para>
  367. 很希望你能从这章的信息里发现这个特别的组件的灵活性并且用到你的程序和网站里。
  368. </para>
  369. </sect2>
  370. </sect1>
  371. <!--
  372. vim:se ts=4 sw=4 et:
  373. -->