quickstart-create-project.xml 15 KB

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