quickstart-create-project.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.quickstart.create-project">
  4. <title>Create Your Project</title>
  5. <para>
  6. In order to create your project, you must first download and extract Zend Framework.
  7. </para>
  8. <sect2 id="learning.quickstart.create-project.install-zf">
  9. <title>Install Zend Framework</title>
  10. <para>
  11. The easiest way to get Zend Framework along with a complete PHP stack is by installing
  12. <ulink url="http://www.zend.com/en/products/server-ce/downloads">Zend Server</ulink>.
  13. Zend Server has native installers for Mac OSX, Windows, Fedora Core, and Ubuntu, as well
  14. as a universal installation package compatible with most Linux distributions.
  15. </para>
  16. <para>
  17. After you have installed Zend Server, the Framework files may be found
  18. under <filename>/usr/local/zend/share/ZendFramework</filename> on Mac OSX and Linux,
  19. and <filename>C:\Program Files\Zend\ZendServer\share\ZendFramework</filename> on
  20. Windows. The <constant>include_path</constant> will already be configured to include
  21. Zend Framework.
  22. </para>
  23. <para>
  24. Alternately, you can <ulink url="http://framework.zend.com/download/latest">Download the
  25. latest version of Zend Framework</ulink> and extract the contents; make a note of where
  26. you have done so.
  27. </para>
  28. <para>
  29. Optionally, you can add the path to the <filename>library/</filename> subdirectory of
  30. the archive to your <filename>php.ini</filename>'s <constant>include_path</constant>
  31. setting.
  32. </para>
  33. <para>
  34. That's it! Zend Framework is now installed and ready to use.
  35. </para>
  36. </sect2>
  37. <sect2 id="learning.quickstart.create-project.create-project">
  38. <title>Create Your Project</title>
  39. <note>
  40. <title>zf Command Line Tool</title>
  41. <para>
  42. In your Zend Framework installation is a <filename>bin/</filename> subdirectory,
  43. containing the scripts <filename>zf.sh</filename> and <filename>zf.bat</filename>
  44. for Unix-based and Windows-based users, respectively. Make a note of the absolute
  45. path to this script.
  46. </para>
  47. <para>
  48. Wherever you see references to <filename>zf.sh</filename> or
  49. <filename>zf.bat</filename>, please substitute the absolute path to the script. On
  50. Unix-like systems, you may want to use your shell's alias functionality:
  51. <command>alias zf.sh=path/to/ZendFramework/bin/zf.sh</command>.
  52. </para>
  53. <para>
  54. If you have problems setting up the <command>zf</command> command-line tool, please
  55. refer to <link linkend="zend.tool.framework.clitool.setup-general">the
  56. manual</link>.
  57. </para>
  58. </note>
  59. <para>
  60. Open a terminal (in Windows, <command>Start -> Run</command>, and then use
  61. <command>cmd</command>). Navigate to a directory where you would like to start a
  62. project. Then, use the path to the appropriate script, and execute one of the following:
  63. </para>
  64. <programlisting language="shell"><![CDATA[
  65. # Unix:
  66. % zf.sh create project quickstart
  67. # DOS/Windows:
  68. C:> zf.bat create project quickstart
  69. ]]></programlisting>
  70. <para>
  71. Running this command will create your basic site structure, including your initial
  72. controllers and views. The tree looks like the following:
  73. </para>
  74. <programlisting language="text"><![CDATA[
  75. quickstart
  76. |-- application
  77. | |-- Bootstrap.php
  78. | |-- configs
  79. | | `-- application.ini
  80. | |-- controllers
  81. | | |-- ErrorController.php
  82. | | `-- IndexController.php
  83. | |-- models
  84. | `-- views
  85. | |-- helpers
  86. | `-- scripts
  87. | |-- error
  88. | | `-- error.phtml
  89. | `-- index
  90. | `-- index.phtml
  91. |-- library
  92. |-- public
  93. | `-- index.php
  94. `-- tests
  95. |-- application
  96. | `-- bootstrap.php
  97. |-- library
  98. | `-- bootstrap.php
  99. `-- phpunit.xml
  100. ]]></programlisting>
  101. <para>
  102. At this point, if you haven't added Zend Framework to your
  103. <constant>include_path</constant>, we recommend either copying or symlinking it into
  104. your <filename>library/</filename> directory. In either case, you'll want to either
  105. recursively copy or symlink the <filename>library/Zend/</filename> directory of your
  106. Zend Framework installation into the <filename>library/</filename> directory of your
  107. project. On unix-like systems, that would look like one of the following:
  108. </para>
  109. <programlisting language="shell"><![CDATA[
  110. # Symlink:
  111. % cd library; ln -s path/to/ZendFramework/library/Zend .
  112. # Copy:
  113. % cd library; cp -r path/to/ZendFramework/library/Zend .
  114. ]]></programlisting>
  115. <para>
  116. On Windows systems, it may be easiest to do this from the Explorer.
  117. </para>
  118. <para>
  119. Now that the project is created, the main artifacts to begin understanding are the
  120. bootstrap, configuration, action controllers, and views.
  121. </para>
  122. </sect2>
  123. <sect2 id="learning.quickstart.create-project.bootstrap">
  124. <title>The Bootstrap</title>
  125. <para>
  126. Your <classname>Bootstrap</classname> class defines what resources and components to
  127. initialize. By default, Zend Framework's <link linkend="zend.controller.front">Front
  128. Controller</link> is initialized, and it uses the
  129. <filename>application/controllers/</filename> as the default directory in which to look
  130. for action controllers (more on that later). The class looks like the following:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. // application/Bootstrap.php
  134. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  135. {
  136. }
  137. ]]></programlisting>
  138. <para>
  139. As you can see, not much is necessary to begin with.
  140. </para>
  141. </sect2>
  142. <sect2 id="learning.quickstart.create-project.configuration">
  143. <title>Configuration</title>
  144. <para>
  145. While Zend Framework is itself configurationless, you often need to configure your
  146. application. The default configuration is placed in
  147. <filename>application/configs/application.ini</filename>, and contains some basic
  148. directives for setting your PHP environment (for instance, turning error reporting on
  149. and off), indicating the path to your bootstrap class (as well as its class name), and
  150. the path to your action controllers. It looks as follows:
  151. </para>
  152. <programlisting language="ini"><![CDATA[
  153. ; application/configs/application.ini
  154. [production]
  155. phpSettings.display_startup_errors = 0
  156. phpSettings.display_errors = 0
  157. includePaths.library = APPLICATION_PATH "/../library"
  158. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  159. bootstrap.class = "Bootstrap"
  160. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  161. [staging : production]
  162. [testing : production]
  163. phpSettings.display_startup_errors = 1
  164. phpSettings.display_errors = 1
  165. [development : production]
  166. phpSettings.display_startup_errors = 1
  167. phpSettings.display_errors = 1
  168. ]]></programlisting>
  169. <para>
  170. Several things about this file should be noted. First, when using INI-style
  171. configuration, you can reference constants directly and expand them;
  172. <constant>APPLICATION_PATH</constant> is actually a constant. Additionally note that
  173. there are several sections defined: production, staging, testing, and development. The
  174. latter three inherit settings from the "production" environment. This is a useful way to
  175. organize configuration to ensure that appropriate settings are available in each stage
  176. of application development.
  177. </para>
  178. </sect2>
  179. <sect2 id="learning.quickstart.create-project.action-controllers">
  180. <title>Action Controllers</title>
  181. <para>
  182. Your application's <emphasis>action controllers</emphasis> contain your application
  183. workflow, and do the work of mapping your requests to the appropriate models and views.
  184. </para>
  185. <para>
  186. An action controller should have one or more methods ending in "Action"; these methods
  187. may then be requested via the web. By default, Zend Framework URLs follow the schema
  188. <constant>/controller/action</constant>, where "controller" maps to the action
  189. controller name (minus the "Controller" suffix) and "action" maps to an action method
  190. (minus the "Action" suffix).
  191. </para>
  192. <para>
  193. Typically, you always need an <classname>IndexController</classname>, which is a
  194. fallback controller and which also serves the home page of the site, and an
  195. <classname>ErrorController</classname>, which is used to indicate things such as HTTP
  196. 404 errors (controller or action not found) and HTTP 500 errors (application errors).
  197. </para>
  198. <para>
  199. The default <classname>IndexController</classname> is as follows:
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. // application/controllers/IndexController.php
  203. class IndexController extends Zend_Controller_Action
  204. {
  205. public function init()
  206. {
  207. /* Initialize action controller here */
  208. }
  209. public function indexAction()
  210. {
  211. // action body
  212. }
  213. }
  214. ]]></programlisting>
  215. <para>
  216. And the default <classname>ErrorController</classname> is as follows:
  217. </para>
  218. <programlisting language="php"><![CDATA[
  219. // application/controllers/ErrorController.php
  220. class ErrorController extends Zend_Controller_Action
  221. {
  222. public function errorAction()
  223. {
  224. $errors = $this->_getParam('error_handler');
  225. switch ($errors->type) {
  226. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  227. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  228. // 404 error -- controller or action not found
  229. $this->getResponse()->setHttpResponseCode(404);
  230. $this->view->message = 'Page not found';
  231. break;
  232. default:
  233. // application error
  234. $this->getResponse()->setHttpResponseCode(500);
  235. $this->view->message = 'Application error';
  236. break;
  237. }
  238. $this->view->exception = $errors->exception;
  239. $this->view->request = $errors->request;
  240. }
  241. }
  242. ]]></programlisting>
  243. <para>
  244. You'll note that (1) the <classname>IndexController</classname> contains no real code,
  245. and (2) the <classname>ErrorController</classname> makes reference to a "view" property.
  246. That leads nicely into our next subject.
  247. </para>
  248. </sect2>
  249. <sect2 id="learning.quickstart.create-project.views">
  250. <title>Views</title>
  251. <para>
  252. Views in Zend Framework are written in plain old PHP. View scripts are placed in
  253. <filename>application/views/scripts/</filename>, where they are further categorized
  254. using the controller names. In our case, we have an
  255. <classname>IndexController</classname> and an <classname>ErrorController</classname>,
  256. and thus we have corresponding <filename>index/</filename> and
  257. <filename>error/</filename> subdirectories within our view scripts directory. Within
  258. these subdirectories, you will then find and create view scripts that correspond to each
  259. controller action exposed; in the default case, we thus have the view scripts
  260. <filename>index/index.phtml</filename> and <filename>error/error.phtml</filename>.
  261. </para>
  262. <para>
  263. View scripts may contain any markup you want, and use the <code>&lt;?php</code> opening
  264. tag and <code>?&gt;</code> closing tag to insert PHP directives.
  265. </para>
  266. <para>
  267. The following is what we install by default for the
  268. <filename>index/index.phtml</filename> view script:
  269. </para>
  270. <programlisting language="php"><![CDATA[
  271. <!-- application/views/scripts/index/index.phtml -->
  272. <style>
  273. a:link,
  274. a:visited
  275. {
  276. color: #0398CA;
  277. }
  278. span#zf-name
  279. {
  280. color: #91BE3F;
  281. }
  282. div#welcome
  283. {
  284. color: #FFFFFF;
  285. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  286. width: 600px;
  287. height: 400px;
  288. border: 2px solid #444444;
  289. overflow: hidden;
  290. text-align: center;
  291. }
  292. div#more-information
  293. {
  294. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  295. height: 100%;
  296. }
  297. </style>
  298. <div id="welcome">
  299. <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />
  300. <h3>This is your project's main page<h3 />
  301. <div id="more-information">
  302. <p>
  303. <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />
  304. </p>
  305. <p>
  306. Helpful Links: <br />
  307. <a href="http://framework.zend.com/">Zend Framework Website</a> |
  308. <a href="http://framework.zend.com/manual/en/">Zend Framework
  309. Manual</a>
  310. </p>
  311. </div>
  312. </div>
  313. ]]></programlisting>
  314. <para>
  315. The <filename>error/error.phtml</filename> view script is slightly more interesting as
  316. it uses some PHP conditionals:
  317. </para>
  318. <programlisting language="php"><![CDATA[
  319. <!-- application/views/scripts/error/error.phtml -->
  320. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";
  321. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
  322. <html xmlns="http://www.w3.org/1999/xhtml">
  323. <head>
  324. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  325. <title>Zend Framework Default Application</title>
  326. </head>
  327. <body>
  328. <h1>An error occurred</h1>
  329. <h2><?php echo $this->message ?></h2>
  330. <?php if ('development' == $this->env): ?>
  331. <h3>Exception information:</h3>
  332. <p>
  333. <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  334. </p>
  335. <h3>Stack trace:</h3>
  336. <pre><?php echo $this->exception->getTraceAsString() ?>
  337. </pre>
  338. <h3>Request Parameters:</h3>
  339. <pre><?php echo var_export($this->request->getParams(), 1) ?>
  340. </pre>
  341. <?php endif ?>
  342. </body>
  343. </html>
  344. ]]></programlisting>
  345. </sect2>
  346. <sect2 id="learning.quickstart.create-project.checkpoint">
  347. <title>Checkpoint</title>
  348. <para>
  349. At this point, you should be able to fire up your initial Zend Framework application.
  350. Create a virtual host in your web server, and point its document root to your
  351. application's <filename>public/</filename> subdirectory. Make sure your host's name is
  352. in your DNS or hosts file, and then point your browser to it. You should be able to see
  353. a welcome page at this point.
  354. </para>
  355. </sect2>
  356. </sect1>