Zend_Application-QuickStart.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.application.quick-start">
  4. <title>Zend_Application Quick Start</title>
  5. <para>
  6. There are two paths to getting started with
  7. <classname>Zend_Application</classname>, and they depend on how you start your
  8. project. In each case, you always start with creating a
  9. <classname>Bootstrap</classname> class, and a related configuration file.
  10. </para>
  11. <para>
  12. If you plan on using <classname>Zend_Tool</classname> to create your project,
  13. continue reading below. If you will be adding
  14. <classname>Zend_Application</classname> to an existing project, you'll want to
  15. <link linkend="zend.application.quick-start.manual">skip ahead</link>.
  16. </para>
  17. <sect2 id="zend.application.quick-start.zend-tool">
  18. <title>Using Zend_Tool</title>
  19. <para>
  20. The quickest way to start using <classname>Zend_Application</classname> is to use
  21. <classname>Zend_Tool</classname> to generate your project. This will also create
  22. your <classname>Bootstrap</classname> class and file.
  23. </para>
  24. <para>
  25. To create a project, execute the <command>zf</command> command (on *nix systems):
  26. </para>
  27. <programlisting language="sh"><![CDATA[
  28. % zf create project newproject
  29. ]]></programlisting>
  30. <para>
  31. Or the Windows <filename>zf.bat</filename> command:
  32. </para>
  33. <programlisting language="dos"><![CDATA[
  34. C:> zf.bat create project newproject
  35. ]]></programlisting>
  36. <para>
  37. Both will create a project structure that looks like the following:
  38. </para>
  39. <programlisting language="text"><![CDATA[
  40. newproject
  41. |-- application
  42. | |-- Bootstrap.php
  43. | |-- configs
  44. | | `-- application.ini
  45. | |-- controllers
  46. | | |-- ErrorController.php
  47. | | `-- IndexController.php
  48. | |-- models
  49. | `-- views
  50. | |-- helpers
  51. | `-- scripts
  52. | |-- error
  53. | | `-- error.phtml
  54. | `-- index
  55. | `-- index.phtml
  56. |-- library
  57. |-- public
  58. | `-- index.php
  59. `-- tests
  60. |-- application
  61. | `-- bootstrap.php
  62. |-- library
  63. | `-- bootstrap.php
  64. `-- phpunit.xml
  65. ]]></programlisting>
  66. <para>
  67. In the above diagram, your bootstrap is in
  68. <filename>newproject/application/Bootstrap.php</filename>, and looks like
  69. the following at first:
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  73. {
  74. }
  75. ]]></programlisting>
  76. <para>
  77. You'll also note that a configuration file,
  78. <filename>newproject/application/configs/application.ini</filename>, is
  79. created. It has the following contents:
  80. </para>
  81. <programlisting language="dosini"><![CDATA[
  82. [production]
  83. phpSettings.display_startup_errors = 0
  84. phpSettings.display_errors = 0
  85. includePaths.library = APPLICATION_PATH "/../library"
  86. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  87. bootstrap.class = "Bootstrap"
  88. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  89. [staging : production]
  90. [testing : production]
  91. phpSettings.display_startup_errors = 1
  92. phpSettings.display_errors = 1
  93. [development : production]
  94. phpSettings.display_startup_errors = 1
  95. phpSettings.display_errors = 1
  96. ]]></programlisting>
  97. <para>
  98. All settings in this configuration file are for use with
  99. <classname>Zend_Application</classname> and your bootstrap.
  100. </para>
  101. <para>
  102. Another file of interest is the
  103. <filename>newproject/public/index.php</filename> file, which invokes
  104. <classname>Zend_Application</classname> and dispatches it.
  105. </para>
  106. <programlisting language="php"><![CDATA[
  107. // Define path to application directory
  108. defined('APPLICATION_PATH')
  109. || define('APPLICATION_PATH',
  110. realpath(dirname(__FILE__) . '/../application'));
  111. // Define application environment
  112. defined('APPLICATION_ENV')
  113. || define('APPLICATION_ENV',
  114. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  115. : 'production'));
  116. /** Zend_Application */
  117. require_once 'Zend/Application.php';
  118. // Create application, bootstrap, and run
  119. $application = new Zend_Application(
  120. APPLICATION_ENV,
  121. APPLICATION_PATH . '/configs/application.ini'
  122. );
  123. $application->bootstrap()
  124. ->run();
  125. ]]></programlisting>
  126. <para>
  127. To continue the quick start, please <link
  128. linkend="zend.application.quick-start.resources">skip to the
  129. Resources section</link>.
  130. </para>
  131. </sect2>
  132. <sect2 id="zend.application.quick-start.manual">
  133. <title>Adding Zend_Application to your application</title>
  134. <para>
  135. The basics of <classname>Zend_Application</classname> are fairly simple:
  136. </para>
  137. <itemizedlist>
  138. <listitem>
  139. <para>
  140. Create an <filename>application/Bootstrap.php</filename> file, with the
  141. class <classname>Bootstrap</classname>.
  142. </para>
  143. </listitem>
  144. <listitem>
  145. <para>
  146. Create an <filename>application/configs/application.ini</filename>
  147. configuration file with the base configuration necessary for
  148. <classname>Zend_Application</classname>.
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. Modify your <filename>public/index.php</filename> to utilize
  154. <classname>Zend_Application</classname>.
  155. </para>
  156. </listitem>
  157. </itemizedlist>
  158. <para>
  159. First, create your <classname>Bootstrap</classname> class. Create a file,
  160. <filename>application/Bootstrap.php</filename>, with the following contents:
  161. </para>
  162. <programlisting language="php"><![CDATA[
  163. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  164. {
  165. }
  166. ]]></programlisting>
  167. <para>
  168. Now, create your configuration. For this tutorial, we will use an
  169. <acronym>INI</acronym> style configuration; you may, of course, use an
  170. <acronym>XML</acronym>, <acronym>JSON</acronym>, <acronym>YAML</acronym>, or
  171. <acronym>PHP</acronym> configuration file as well. Create the file
  172. <filename>application/configs/application.ini</filename>, and provide the following
  173. contents:
  174. </para>
  175. <programlisting language="dosini"><![CDATA[
  176. [production]
  177. phpSettings.display_startup_errors = 0
  178. phpSettings.display_errors = 0
  179. includePaths.library = APPLICATION_PATH "/../library"
  180. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  181. bootstrap.class = "Bootstrap"
  182. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  183. [staging : production]
  184. [testing : production]
  185. phpSettings.display_startup_errors = 1
  186. phpSettings.display_errors = 1
  187. [development : production]
  188. phpSettings.display_startup_errors = 1
  189. phpSettings.display_errors = 1
  190. ]]></programlisting>
  191. <para>
  192. Now, let's modify your gateway script,
  193. <filename>public/index.php</filename>. If the file does not exist, create
  194. it; otherwise, replace it with the following contents:
  195. </para>
  196. <programlisting language="php"><![CDATA[
  197. // Define path to application directory
  198. defined('APPLICATION_PATH')
  199. || define('APPLICATION_PATH',
  200. realpath(dirname(__FILE__) . '/../application'));
  201. // Define application environment
  202. defined('APPLICATION_ENV')
  203. || define('APPLICATION_ENV',
  204. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  205. : 'production'));
  206. // Typically, you will also want to add your library/ directory
  207. // to the include_path, particularly if it contains your ZF installed
  208. set_include_path(implode(PATH_SEPARATOR, array(
  209. dirname(dirname(__FILE__)) . '/library',
  210. get_include_path(),
  211. )));
  212. /** Zend_Application */
  213. require_once 'Zend/Application.php';
  214. // Create application, bootstrap, and run
  215. $application = new Zend_Application(
  216. APPLICATION_ENV,
  217. APPLICATION_PATH . '/configs/application.ini'
  218. );
  219. $application->bootstrap()
  220. ->run();
  221. ]]></programlisting>
  222. <para>
  223. You may note that the application environment constant value looks
  224. for an environment variable "APPLICATION_ENV". We recommend setting
  225. this in your web server environment. In Apache, you can set this
  226. either in your vhost definition, or in your <filename>.htaccess</filename>
  227. file. We recommend the following contents for your
  228. <filename>public/.htaccess</filename> file:
  229. </para>
  230. <programlisting language="conf"><![CDATA[
  231. SetEnv APPLICATION_ENV development
  232. RewriteEngine On
  233. RewriteCond %{REQUEST_FILENAME} -s [OR]
  234. RewriteCond %{REQUEST_FILENAME} -l [OR]
  235. RewriteCond %{REQUEST_FILENAME} -d
  236. RewriteRule ^.*$ - [NC,L]
  237. RewriteRule ^.*$ index.php [NC,L]
  238. ]]></programlisting>
  239. <note>
  240. <title>Learn about mod_rewrite</title>
  241. <para>
  242. The above rewrite rules allow access to any file under your
  243. virtual host's document root. If there are files you do not want
  244. exposed in this way, you may want to be more restrictive in your
  245. rules. Go to the Apache website to <ulink
  246. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">learn
  247. more about mod_rewrite</ulink>.
  248. </para>
  249. </note>
  250. <para>
  251. At this point, you're all set to start taking advantage of
  252. <classname>Zend_Application</classname>.
  253. </para>
  254. </sect2>
  255. <sect2 id="zend.application.quick-start.resources">
  256. <title>Adding and creating resources</title>
  257. <para>
  258. If you followed the directions above, then your bootstrap class
  259. will be utilizing a front controller, and when it is run, it will
  260. dispatch the front controller. However, in all likelihood, you'll
  261. need a little more configuration than this.
  262. </para>
  263. <para>
  264. In this section, we'll look at adding two resources to your
  265. application. First, we'll set up your layouts, and then we'll
  266. customize your view object.
  267. </para>
  268. <para>
  269. One of the standard resources provided with
  270. <classname>Zend_Application</classname> is the "layout" resource. This
  271. resource expects you to define configuration values which it will
  272. then use to configure your <classname>Zend_Layout</classname> instance.
  273. </para>
  274. <para>
  275. To use it, all we need to do is update the configuration file.
  276. </para>
  277. <programlisting language="dosini"><![CDATA[
  278. [production]
  279. phpSettings.display_startup_errors = 0
  280. phpSettings.display_errors = 0
  281. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  282. bootstrap.class = "Bootstrap"
  283. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  284. ; ADD THE FOLLOWING LINES
  285. resources.layout.layout = "layout"
  286. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  287. [staging : production]
  288. [testing : production]
  289. phpSettings.display_startup_errors = 1
  290. phpSettings.display_errors = 1
  291. [development : production]
  292. phpSettings.display_startup_errors = 1
  293. phpSettings.display_errors = 1
  294. ]]></programlisting>
  295. <para>
  296. If you haven't already, create the directory
  297. <filename>application/layouts/scripts/</filename>, and the file
  298. <filename>layout.phtml</filename> within that directory. A good starting
  299. layout is as follows (and ties in with the view resource covered
  300. next):
  301. </para>
  302. <programlisting language="php"><![CDATA[
  303. <?php echo $this->doctype() ?>
  304. <html>
  305. <head>
  306. <?php echo $this->headTitle() ?>
  307. <?php echo $this->headLink() ?>
  308. <?php echo $this->headStyle() ?>
  309. <?php echo $this->headScript() ?>
  310. </head>
  311. <body>
  312. <?php echo $this->layout()->content ?>
  313. </body>
  314. </html>
  315. ]]></programlisting>
  316. <para>
  317. At this point, you will now have a working layout.
  318. </para>
  319. <para>
  320. Now, we'll add a custom view resource. When initializing the view,
  321. we'll want to set the <acronym>HTML</acronym> DocType and a default value for the title
  322. to use in the <acronym>HTML</acronym> head. This can be accomplished by editing your
  323. <classname>Bootstrap</classname> class to add a method:
  324. </para>
  325. <programlisting language="php"><![CDATA[
  326. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  327. {
  328. protected function _initView()
  329. {
  330. // Initialize view
  331. $view = new Zend_View();
  332. $view->doctype('XHTML1_STRICT');
  333. $view->headTitle('My First Zend Framework Application');
  334. // Add it to the ViewRenderer
  335. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  336. 'ViewRenderer'
  337. );
  338. $viewRenderer->setView($view);
  339. // Return it, so that it can be stored by the bootstrap
  340. return $view;
  341. }
  342. }
  343. ]]></programlisting>
  344. <para>
  345. This method will be automatically executed when you bootstrap the
  346. application, and will ensure your view is initialized according to
  347. your application needs.
  348. </para>
  349. </sect2>
  350. <sect2 id="zend.application.quick-start.next-steps">
  351. <title>Next steps with Zend_Application</title>
  352. <para>
  353. The above should get you started with <classname>Zend_Application</classname>
  354. and creating your application bootstrap. From here, you should start
  355. creating resource methods, or, for maximum re-usability, resource
  356. plugin classes. Continue reading to learn more!
  357. </para>
  358. </sect2>
  359. </sect1>