Zend_Application-QuickStart.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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><para>
  139. Create an <filename>application/Bootstrap.php</filename> file, with the
  140. class <classname>Bootstrap</classname>.
  141. </para></listitem>
  142. <listitem><para>
  143. Create an <filename>application/configs/application.ini</filename>
  144. configuration file with the base configuration necessary for
  145. <classname>Zend_Application</classname>.
  146. </para></listitem>
  147. <listitem><para>
  148. Modify your <filename>public/index.php</filename> to utilize
  149. <classname>Zend_Application</classname>.
  150. </para></listitem>
  151. </itemizedlist>
  152. <para>
  153. First, create your <classname>Bootstrap</classname> class. Create a file,
  154. <filename>application/Bootstrap.php</filename>, with the following contents:
  155. </para>
  156. <programlisting language="php"><![CDATA[
  157. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  158. {
  159. }
  160. ]]></programlisting>
  161. <para>
  162. Now, create your configuration. For this tutorial, we will use an
  163. <acronym>INI</acronym> style configuration; you may, of course, use an
  164. <acronym>XML</acronym> or <acronym>PHP</acronym> configuration file as well. Create
  165. the file <filename>application/configs/application.ini</filename>, and provide the
  166. following contents:
  167. </para>
  168. <programlisting language="dosini"><![CDATA[
  169. [production]
  170. phpSettings.display_startup_errors = 0
  171. phpSettings.display_errors = 0
  172. includePaths.library = APPLICATION_PATH "/../library"
  173. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  174. bootstrap.class = "Bootstrap"
  175. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  176. [staging : production]
  177. [testing : production]
  178. phpSettings.display_startup_errors = 1
  179. phpSettings.display_errors = 1
  180. [development : production]
  181. phpSettings.display_startup_errors = 1
  182. phpSettings.display_errors = 1
  183. ]]></programlisting>
  184. <para>
  185. Now, let's modify your gateway script,
  186. <filename>public/index.php</filename>. If the file does not exist, create
  187. it; otherwise, replace it with the following contents:
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. // Define path to application directory
  191. defined('APPLICATION_PATH')
  192. || define('APPLICATION_PATH',
  193. realpath(dirname(__FILE__) . '/../application'));
  194. // Define application environment
  195. defined('APPLICATION_ENV')
  196. || define('APPLICATION_ENV',
  197. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  198. : 'production'));
  199. // Typically, you will also want to add your library/ directory
  200. // to the include_path, particularly if it contains your ZF installed
  201. set_include_path(implode(PATH_SEPARATOR, array(
  202. dirname(dirname(__FILE__)) . '/library',
  203. get_include_path(),
  204. )));
  205. /** Zend_Application */
  206. require_once 'Zend/Application.php';
  207. // Create application, bootstrap, and run
  208. $application = new Zend_Application(
  209. APPLICATION_ENV,
  210. APPLICATION_PATH . '/configs/application.ini'
  211. );
  212. $application->bootstrap()
  213. ->run();
  214. ]]></programlisting>
  215. <para>
  216. You may note that the application environment constant value looks
  217. for an environment variable "APPLICATION_ENV". We recommend setting
  218. this in your web server environment. In Apache, you can set this
  219. either in your vhost definition, or in your <filename>.htaccess</filename>
  220. file. We recommend the following contents for your
  221. <filename>public/.htaccess</filename> file:
  222. </para>
  223. <programlisting language="conf"><![CDATA[
  224. SetEnv APPLICATION_ENV development
  225. RewriteEngine On
  226. RewriteCond %{REQUEST_FILENAME} -s [OR]
  227. RewriteCond %{REQUEST_FILENAME} -l [OR]
  228. RewriteCond %{REQUEST_FILENAME} -d
  229. RewriteRule ^.*$ - [NC,L]
  230. RewriteRule ^.*$ index.php [NC,L]
  231. ]]></programlisting>
  232. <note>
  233. <title>Learn about mod_rewrite</title>
  234. <para>
  235. The above rewrite rules allow access to any file under your
  236. virtual host's document root. If there are files you do not want
  237. exposed in this way, you may want to be more restrictive in your
  238. rules. Go to the Apache website to <ulink
  239. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">learn
  240. more about mod_rewrite</ulink>.
  241. </para>
  242. </note>
  243. <para>
  244. At this point, you're all set to start taking advantage of
  245. <classname>Zend_Application</classname>.
  246. </para>
  247. </sect2>
  248. <sect2 id="zend.application.quick-start.resources">
  249. <title>Adding and creating resources</title>
  250. <para>
  251. If you followed the directions above, then your bootstrap class
  252. will be utilizing a front controller, and when it is run, it will
  253. dispatch the front controller. However, in all likelihood, you'll
  254. need a little more configuration than this.
  255. </para>
  256. <para>
  257. In this section, we'll look at adding two resources to your
  258. application. First, we'll set up your layouts, and then we'll
  259. customize your view object.
  260. </para>
  261. <para>
  262. One of the standard resources provided with
  263. <classname>Zend_Application</classname> is the "layout" resource. This
  264. resource expects you to define configuration values which it will
  265. then use to configure your <classname>Zend_Layout</classname> instance.
  266. </para>
  267. <para>
  268. To use it, all we need to do is update the configuration file.
  269. </para>
  270. <programlisting language="dosini"><![CDATA[
  271. [production]
  272. phpSettings.display_startup_errors = 0
  273. phpSettings.display_errors = 0
  274. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  275. bootstrap.class = "Bootstrap"
  276. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  277. ; ADD THE FOLLOWING LINES
  278. resources.layout.layout = "layout"
  279. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  280. [staging : production]
  281. [testing : production]
  282. phpSettings.display_startup_errors = 1
  283. phpSettings.display_errors = 1
  284. [development : production]
  285. phpSettings.display_startup_errors = 1
  286. phpSettings.display_errors = 1
  287. ]]></programlisting>
  288. <para>
  289. If you haven't already, create the directory
  290. <filename>application/layouts/scripts/</filename>, and the file
  291. <filename>layout.phtml</filename> within that directory. A good starting
  292. layout is as follows (and ties in with the view resource covered
  293. next):
  294. </para>
  295. <programlisting language="php"><![CDATA[
  296. <?php echo $this->doctype() ?>
  297. <html>
  298. <head>
  299. <?php echo $this->headTitle() ?>
  300. <?php echo $this->headLink() ?>
  301. <?php echo $this->headStyle() ?>
  302. <?php echo $this->headScript() ?>
  303. </head>
  304. <body>
  305. <?php echo $this->layout()->content ?>
  306. </body>
  307. </html>
  308. ]]></programlisting>
  309. <para>
  310. At this point, you will now have a working layout.
  311. </para>
  312. <para>
  313. Now, we'll add a custom view resource. When initializing the view,
  314. we'll want to set the <acronym>HTML</acronym> DocType and a default value for the title
  315. to use in the <acronym>HTML</acronym> head. This can be accomplished by editing your
  316. <classname>Bootstrap</classname> class to add a method:
  317. </para>
  318. <programlisting language="php"><![CDATA[
  319. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  320. {
  321. protected function _initView()
  322. {
  323. // Initialize view
  324. $view = new Zend_View();
  325. $view->doctype('XHTML1_STRICT');
  326. $view->headTitle('My First Zend Framework Application');
  327. // Add it to the ViewRenderer
  328. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  329. 'ViewRenderer'
  330. );
  331. $viewRenderer->setView($view);
  332. // Return it, so that it can be stored by the bootstrap
  333. return $view;
  334. }
  335. }
  336. ]]></programlisting>
  337. <para>
  338. This method will be automatically executed when you bootstrap the
  339. application, and will ensure your view is initialized according to
  340. your application needs.
  341. </para>
  342. </sect2>
  343. <sect2 id="zend.application.quick-start.next-steps">
  344. <title>Next steps with Zend_Application</title>
  345. <para>
  346. The above should get you started with <classname>Zend_Application</classname>
  347. and creating your application bootstrap. From here, you should start
  348. creating resource methods, or, for maximum re-usability, resource
  349. plugin classes. Continue reading to learn more!
  350. </para>
  351. </sect2>
  352. </sect1>