quickstart-create-project.xml 15 KB

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