quickstart-create-project.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 the command <command>zf</command>, please substitute
  49. the absolute path to the script. On Unix-like systems, you may want to use your
  50. shell's alias functionality: <command>alias
  51. 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. % zf create project quickstart
  66. ]]></programlisting>
  67. <para>
  68. Running this command will create your basic site structure, including your initial
  69. controllers and views. The tree looks like the following:
  70. </para>
  71. <programlisting language="text"><![CDATA[
  72. quickstart
  73. |-- application
  74. | |-- Bootstrap.php
  75. | |-- configs
  76. | | `-- application.ini
  77. | |-- controllers
  78. | | |-- ErrorController.php
  79. | | `-- IndexController.php
  80. | |-- models
  81. | `-- views
  82. | |-- helpers
  83. | `-- scripts
  84. | |-- error
  85. | | `-- error.phtml
  86. | `-- index
  87. | `-- index.phtml
  88. |-- library
  89. |-- public
  90. | |-- .htaccess
  91. | `-- index.php
  92. `-- tests
  93. |-- application
  94. | `-- bootstrap.php
  95. |-- library
  96. | `-- bootstrap.php
  97. `-- phpunit.xml
  98. ]]></programlisting>
  99. <para>
  100. At this point, if you haven't added Zend Framework to your
  101. <constant>include_path</constant>, we recommend either copying or symlinking it into
  102. your <filename>library/</filename> directory. In either case, you'll want to either
  103. recursively copy or symlink the <filename>library/Zend/</filename> directory of your
  104. Zend Framework installation into the <filename>library/</filename> directory of your
  105. project. On unix-like systems, that would look like one of the following:
  106. </para>
  107. <programlisting language="shell"><![CDATA[
  108. # Symlink:
  109. % cd library; ln -s path/to/ZendFramework/library/Zend .
  110. # Copy:
  111. % cd library; cp -r path/to/ZendFramework/library/Zend .
  112. ]]></programlisting>
  113. <para>
  114. On Windows systems, it may be easiest to do this from the Explorer.
  115. </para>
  116. <para>
  117. Now that the project is created, the main artifacts to begin understanding are the
  118. bootstrap, configuration, action controllers, and views.
  119. </para>
  120. </sect2>
  121. <sect2 id="learning.quickstart.create-project.bootstrap">
  122. <title>The Bootstrap</title>
  123. <para>
  124. Your <classname>Bootstrap</classname> class defines what resources and components to
  125. initialize. By default, Zend Framework's <link linkend="zend.controller.front">Front
  126. Controller</link> is initialized, and it uses the
  127. <filename>application/controllers/</filename> as the default directory in which to look
  128. for action controllers (more on that later). The class looks like the following:
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. // application/Bootstrap.php
  132. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  133. {
  134. }
  135. ]]></programlisting>
  136. <para>
  137. As you can see, not much is necessary to begin with.
  138. </para>
  139. </sect2>
  140. <sect2 id="learning.quickstart.create-project.configuration">
  141. <title>Configuration</title>
  142. <para>
  143. While Zend Framework is itself configurationless, you often need to configure your
  144. application. The default configuration is placed in
  145. <filename>application/configs/application.ini</filename>, and contains some basic
  146. directives for setting your PHP environment (for instance, turning error reporting on
  147. and off), indicating the path to your bootstrap class (as well as its class name), and
  148. the path to your action controllers. It looks as follows:
  149. </para>
  150. <programlisting language="ini"><![CDATA[
  151. ; application/configs/application.ini
  152. [production]
  153. phpSettings.display_startup_errors = 0
  154. phpSettings.display_errors = 0
  155. includePaths.library = APPLICATION_PATH "/../library"
  156. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  157. bootstrap.class = "Bootstrap"
  158. appnamespace = "Application"
  159. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  160. resources.frontController.params.displayExceptions = 0
  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_ROUTE:
  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.vhost">
  348. <title>Create a virtual host</title>
  349. <para>
  350. For purposes of this quick start, we will assume you are using the <ulink
  351. url="http://httpd.apache.org/">Apache web server</ulink>. Zend Framework works
  352. perfectly well with other web servers -- including Microsoft Internet Information Server,
  353. lighttpd, nginx, and more -- but most developers should be famililar with Apache at the
  354. minimum, and it provides an easy introduction to Zend Framework's directory structure
  355. and rewrite capabilities.
  356. </para>
  357. <para>
  358. To create your vhost, you need to know the location of your
  359. <filename>httpd.conf</filename> file, and potentially where other configuration files
  360. are located. Some common locations:
  361. </para>
  362. <itemizedlist>
  363. <listitem>
  364. <para>
  365. <filename>/etc/httpd/httpd.conf</filename> (Fedora, RHEL, and others)
  366. </para>
  367. </listitem>
  368. <listitem>
  369. <para>
  370. <filename>/etc/apache2/httpd.conf</filename> (Debian, Ubuntu, and others)
  371. </para>
  372. </listitem>
  373. <listitem>
  374. <para>
  375. <filename>/usr/local/zend/etc/httpd.conf</filename> (Zend Server on *nix
  376. machines)
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. <filename>C:\Program Files\Zend\Apache2\conf</filename> (Zend Server on Windows
  382. machines)
  383. </para>
  384. </listitem>
  385. </itemizedlist>
  386. <para>
  387. Within your <filename>httpd.conf</filename> (or <filename>httpd-vhosts.conf</filename>
  388. on some systems), you will need to do two things. First, ensure that the
  389. <varname>NameVirtualHost</varname> is defined; typically, you will set it to a value of
  390. "*:80". Second, define a virtual host:
  391. </para>
  392. <programlisting language="apache"><![CDATA[
  393. <VirtualHost *:80>
  394. ServerName quickstart.local
  395. DocumentRoot /path/to/quickstart/public
  396. SetEnv APPLICATION_ENV "development"
  397. <Directory /path/to/quickstart/public>
  398. DirectoryIndex index.php
  399. AllowOverride All
  400. Order allow,deny
  401. Allow from all
  402. </Directory>
  403. </VirtualHost>
  404. ]]></programlisting>
  405. <para>
  406. There are several things to note. First, note that the <varname>DocumentRoot</varname>
  407. setting specifies the <filename>public</filename> subdirectory of our project; this
  408. means that only files under that directory can ever be served directly by the server.
  409. Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
  410. <varname>Allow</varname> directives; these are to allow us to use
  411. <filename>htacess</filename> files within our project. During development, this is a
  412. good practice, as it prevents the need to constantly restart the web server as you make
  413. changes to your site directives; however, in production, you should likely push the
  414. content of your <filename>htaccess</filename> file into your server configuration and
  415. disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
  416. here is setting an environment variable for your virtual host; this variable will be
  417. picked up in the <filename>index.php</filename> and used to set the
  418. <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
  419. production, you can omit this directive (in which case it will default to the value
  420. "production") or set it explicitly to "production".
  421. </para>
  422. <para>
  423. Finally, you will need to add an entry in your <filename>hosts</filename> file
  424. corresponding to the value you place in your <varname>ServerName</varname> directive. On
  425. *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
  426. typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
  427. the system, the entry will look like the following:
  428. </para>
  429. <programlisting language="text"><![CDATA[
  430. 127.0.0.1 quickstart.local
  431. ]]></programlisting>
  432. <para>
  433. Start your webserver (or restart it), and you should be ready to go.
  434. </para>
  435. </sect2>
  436. <sect2 id="learning.quickstart.create-project.checkpoint">
  437. <title>Checkpoint</title>
  438. <para>
  439. At this point, you should be able to fire up your initial Zend Framework application.
  440. Point your browser to the server name you configured in the previous section; you should
  441. be able to see a welcome page at this point.
  442. </para>
  443. </sect2>
  444. </sect1>