quickstart-create-project.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 <acronym>PHP</acronym> stack
  12. is by installing <ulink url="http://www.zend.com/en/products/server-ce/downloads">Zend
  13. Server</ulink>. Zend Server has native installers for Mac OSX, Windows, Fedora Core,
  14. and Ubuntu, as well as a universal installation package compatible with most Linux
  15. distributions.
  16. </para>
  17. <para>
  18. After you have installed Zend Server, the Framework files may be found
  19. under <filename>/usr/local/zend/share/ZendFramework</filename> on Mac OSX and Linux,
  20. and <filename>C:\Program Files\Zend\ZendServer\share\ZendFramework</filename> on
  21. Windows. 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 the command <command>zf</command>, please substitute
  50. the absolute path to the script. On Unix-like systems, you may want to use your
  51. shell's alias functionality: <command>alias
  52. 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">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. % zf create project quickstart
  67. ]]></programlisting>
  68. <para>
  69. Running this command will create your basic site structure, including your initial
  70. controllers and views. The tree looks like the following:
  71. </para>
  72. <programlisting language="text"><![CDATA[
  73. quickstart
  74. |-- application
  75. | |-- Bootstrap.php
  76. | |-- configs
  77. | | `-- application.ini
  78. | |-- controllers
  79. | | |-- ErrorController.php
  80. | | `-- IndexController.php
  81. | |-- models
  82. | `-- views
  83. | |-- helpers
  84. | `-- scripts
  85. | |-- error
  86. | | `-- error.phtml
  87. | `-- index
  88. | `-- index.phtml
  89. |-- library
  90. |-- public
  91. | |-- .htaccess
  92. | `-- index.php
  93. `-- tests
  94. |-- application
  95. | `-- bootstrap.php
  96. |-- library
  97. | `-- bootstrap.php
  98. `-- phpunit.xml
  99. ]]></programlisting>
  100. <para>
  101. At this point, if you haven't added Zend Framework to your
  102. <constant>include_path</constant>, we recommend either copying or symlinking it into
  103. your <filename>library/</filename> directory. In either case, you'll want to either
  104. recursively copy or symlink the <filename>library/Zend/</filename> directory of your
  105. Zend Framework installation into the <filename>library/</filename> directory of your
  106. project. On unix-like systems, that would look like one of the following:
  107. </para>
  108. <programlisting language="shell"><![CDATA[
  109. # Symlink:
  110. % cd library; ln -s path/to/ZendFramework/library/Zend .
  111. # Copy:
  112. % cd library; cp -r path/to/ZendFramework/library/Zend .
  113. ]]></programlisting>
  114. <para>
  115. On Windows systems, it may be easiest to do this from the Explorer.
  116. </para>
  117. <para>
  118. Now that the project is created, the main artifacts to begin understanding are the
  119. bootstrap, configuration, action controllers, and views.
  120. </para>
  121. </sect2>
  122. <sect2 id="learning.quickstart.create-project.bootstrap">
  123. <title>The Bootstrap</title>
  124. <para>
  125. Your <classname>Bootstrap</classname> class defines what resources and components to
  126. initialize. By default, Zend Framework's <link linkend="zend.controller.front">Front
  127. Controller</link> is initialized, and it uses the
  128. <filename>application/controllers/</filename> as the default directory in which to look
  129. for action controllers (more on that later). The class looks like the following:
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. // application/Bootstrap.php
  133. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  134. {
  135. }
  136. ]]></programlisting>
  137. <para>
  138. As you can see, not much is necessary to begin with.
  139. </para>
  140. </sect2>
  141. <sect2 id="learning.quickstart.create-project.configuration">
  142. <title>Configuration</title>
  143. <para>
  144. While Zend Framework is itself configurationless, you often need to configure your
  145. application. The default configuration is placed in
  146. <filename>application/configs/application.ini</filename>, and contains some basic
  147. directives for setting your <acronym>PHP</acronym> environment (for instance, turning
  148. error reporting on and off), indicating the path to your bootstrap class (as well as its
  149. class name), and the path to your action controllers. It looks as follows:
  150. </para>
  151. <programlisting language="ini"><![CDATA[
  152. ; application/configs/application.ini
  153. [production]
  154. phpSettings.display_startup_errors = 0
  155. phpSettings.display_errors = 0
  156. includePaths.library = APPLICATION_PATH "/../library"
  157. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  158. bootstrap.class = "Bootstrap"
  159. appnamespace = "Application"
  160. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  161. resources.frontController.params.displayExceptions = 0
  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
  172. <acronym>INI</acronym>-style configuration, you can reference constants directly and
  173. expand them; <constant>APPLICATION_PATH</constant> is actually a constant. Additionally
  174. note that there are several sections defined: production, staging, testing, and
  175. development. The latter three inherit settings from the "production" environment. This
  176. is a useful way to organize configuration to ensure that appropriate settings are
  177. available in each stage 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
  197. <acronym>HTTP</acronym> 404 errors (controller or action not found) and
  198. <acronym>HTTP</acronym> 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_ROUTE:
  229. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  230. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  231. // 404 error -- controller or action not found
  232. $this->getResponse()->setHttpResponseCode(404);
  233. $this->view->message = 'Page not found';
  234. break;
  235. default:
  236. // application error
  237. $this->getResponse()->setHttpResponseCode(500);
  238. $this->view->message = 'Application error';
  239. break;
  240. }
  241. $this->view->exception = $errors->exception;
  242. $this->view->request = $errors->request;
  243. }
  244. }
  245. ]]></programlisting>
  246. <para>
  247. You'll note that (1) the <classname>IndexController</classname> contains no real code,
  248. and (2) the <classname>ErrorController</classname> makes reference to a "view" property.
  249. That leads nicely into our next subject.
  250. </para>
  251. </sect2>
  252. <sect2 id="learning.quickstart.create-project.views">
  253. <title>Views</title>
  254. <para>
  255. Views in Zend Framework are written in plain old <acronym>PHP</acronym>. View scripts
  256. are placed in <filename>application/views/scripts/</filename>, where they are further
  257. categorized using the controller names. In our case, we have an
  258. <classname>IndexController</classname> and an <classname>ErrorController</classname>,
  259. and thus we have corresponding <filename>index/</filename> and
  260. <filename>error/</filename> subdirectories within our view scripts directory. Within
  261. these subdirectories, you will then find and create view scripts that correspond to each
  262. controller action exposed; in the default case, we thus have the view scripts
  263. <filename>index/index.phtml</filename> and <filename>error/error.phtml</filename>.
  264. </para>
  265. <para>
  266. View scripts may contain any markup you want, and use the <emphasis>&lt;?php</emphasis>
  267. opening tag and <emphasis>?&gt;</emphasis> closing tag to insert <acronym>PHP</acronym>
  268. directives.
  269. </para>
  270. <para>
  271. The following is what we install by default for the
  272. <filename>index/index.phtml</filename> view script:
  273. </para>
  274. <programlisting language="php"><![CDATA[
  275. <!-- application/views/scripts/index/index.phtml -->
  276. <style>
  277. a:link,
  278. a:visited
  279. {
  280. color: #0398CA;
  281. }
  282. span#zf-name
  283. {
  284. color: #91BE3F;
  285. }
  286. div#welcome
  287. {
  288. color: #FFFFFF;
  289. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  290. width: 600px;
  291. height: 400px;
  292. border: 2px solid #444444;
  293. overflow: hidden;
  294. text-align: center;
  295. }
  296. div#more-information
  297. {
  298. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  299. height: 100%;
  300. }
  301. </style>
  302. <div id="welcome">
  303. <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />
  304. <h3>This is your project's main page<h3 />
  305. <div id="more-information">
  306. <p>
  307. <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />
  308. </p>
  309. <p>
  310. Helpful Links: <br />
  311. <a href="http://framework.zend.com/">Zend Framework Website</a> |
  312. <a href="http://framework.zend.com/manual/en/">Zend Framework
  313. Manual</a>
  314. </p>
  315. </div>
  316. </div>
  317. ]]></programlisting>
  318. <para>
  319. The <filename>error/error.phtml</filename> view script is slightly more interesting as
  320. it uses some <acronym>PHP</acronym> conditionals:
  321. </para>
  322. <programlisting language="php"><![CDATA[
  323. <!-- application/views/scripts/error/error.phtml -->
  324. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";
  325. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
  326. <html xmlns="http://www.w3.org/1999/xhtml">
  327. <head>
  328. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  329. <title>Zend Framework Default Application</title>
  330. </head>
  331. <body>
  332. <h1>An error occurred</h1>
  333. <h2><?php echo $this->message ?></h2>
  334. <?php if ('development' == $this->env): ?>
  335. <h3>Exception information:</h3>
  336. <p>
  337. <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  338. </p>
  339. <h3>Stack trace:</h3>
  340. <pre><?php echo $this->exception->getTraceAsString() ?>
  341. </pre>
  342. <h3>Request Parameters:</h3>
  343. <pre><?php echo var_export($this->request->getParams(), 1) ?>
  344. </pre>
  345. <?php endif ?>
  346. </body>
  347. </html>
  348. ]]></programlisting>
  349. </sect2>
  350. <sect2 id="learning.quickstart.create-project.vhost">
  351. <title>Create a virtual host</title>
  352. <para>
  353. For purposes of this quick start, we will assume you are using the <ulink
  354. url="http://httpd.apache.org/">Apache web server</ulink>. Zend Framework works
  355. perfectly well with other web servers -- including Microsoft Internet Information
  356. Server, lighttpd, nginx, and more -- but most developers should be famililar with Apache
  357. at the minimum, and it provides an easy introduction to Zend Framework's directory
  358. structure and rewrite capabilities.
  359. </para>
  360. <para>
  361. To create your vhost, you need to know the location of your
  362. <filename>httpd.conf</filename> file, and potentially where other configuration files
  363. are located. Some common locations:
  364. </para>
  365. <itemizedlist>
  366. <listitem>
  367. <para>
  368. <filename>/etc/httpd/httpd.conf</filename> (Fedora, RHEL, and others)
  369. </para>
  370. </listitem>
  371. <listitem>
  372. <para>
  373. <filename>/etc/apache2/httpd.conf</filename> (Debian, Ubuntu, and others)
  374. </para>
  375. </listitem>
  376. <listitem>
  377. <para>
  378. <filename>/usr/local/zend/etc/httpd.conf</filename> (Zend Server on *nix
  379. machines)
  380. </para>
  381. </listitem>
  382. <listitem>
  383. <para>
  384. <filename>C:\Program Files\Zend\Apache2\conf</filename> (Zend Server on Windows
  385. machines)
  386. </para>
  387. </listitem>
  388. </itemizedlist>
  389. <para>
  390. Within your <filename>httpd.conf</filename> (or <filename>httpd-vhosts.conf</filename>
  391. on some systems), you will need to do two things. First, ensure that the
  392. <varname>NameVirtualHost</varname> is defined; typically, you will set it to a value of
  393. "*:80". Second, define a virtual host:
  394. </para>
  395. <programlisting language="apache"><![CDATA[
  396. <VirtualHost *:80>
  397. ServerName quickstart.local
  398. DocumentRoot /path/to/quickstart/public
  399. SetEnv APPLICATION_ENV "development"
  400. <Directory /path/to/quickstart/public>
  401. DirectoryIndex index.php
  402. AllowOverride All
  403. Order allow,deny
  404. Allow from all
  405. </Directory>
  406. </VirtualHost>
  407. ]]></programlisting>
  408. <para>
  409. There are several things to note. First, note that the <varname>DocumentRoot</varname>
  410. setting specifies the <filename>public</filename> subdirectory of our project; this
  411. means that only files under that directory can ever be served directly by the server.
  412. Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
  413. <varname>Allow</varname> directives; these are to allow us to use
  414. <filename>htacess</filename> files within our project. During development, this is a
  415. good practice, as it prevents the need to constantly restart the web server as you make
  416. changes to your site directives; however, in production, you should likely push the
  417. content of your <filename>htaccess</filename> file into your server configuration and
  418. disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
  419. here is setting an environment variable for your virtual host; this variable will be
  420. picked up in the <filename>index.php</filename> and used to set the
  421. <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
  422. production, you can omit this directive (in which case it will default to the value
  423. "production") or set it explicitly to "production".
  424. </para>
  425. <para>
  426. Finally, you will need to add an entry in your <filename>hosts</filename> file
  427. corresponding to the value you place in your <varname>ServerName</varname> directive. On
  428. *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
  429. typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
  430. the system, the entry will look like the following:
  431. </para>
  432. <programlisting language="text"><![CDATA[
  433. 127.0.0.1 quickstart.local
  434. ]]></programlisting>
  435. <para>
  436. Start your webserver (or restart it), and you should be ready to go.
  437. </para>
  438. </sect2>
  439. <sect2 id="learning.quickstart.create-project.checkpoint">
  440. <title>Checkpoint</title>
  441. <para>
  442. At this point, you should be able to fire up your initial Zend Framework application.
  443. Point your browser to the server name you configured in the previous section; you should
  444. be able to see a welcome page at this point.
  445. </para>
  446. </sect2>
  447. </sect1>