Zend_Application-QuickStart.xml 13 KB

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