quickstart-create-project.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 24249 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.quickstart.create-project">
  5. <title>Crie Seu Projeto</title>
  6. <para>
  7. Para criar seu projeto, você primeiro deve baixar e extrair o Zend Framework.
  8. </para>
  9. <sect2 id="learning.quickstart.create-project.install-zf">
  10. <title>Instale o Zend Framework</title>
  11. <para>
  12. A maneira mais fácil de obter o Zend Framework junto com uma pilha
  13. <acronym>PHP</acronym> completa é através da instalação do <ulink
  14. url="http://www.zend.com/en/products/server-ce/downloads">Zend Server</ulink>. Zend
  15. Server tem instaladores nativos para Mac OSX, Windows, Fedora Core e Ubuntu, bem como um
  16. pacote de instalação universal compatível com a maioria das distribuições Linux.
  17. </para>
  18. <para>
  19. After you have installed Zend Server, the Framework files may be found
  20. under <filename>/usr/local/zend/share/ZendFramework</filename> on Mac OSX and Linux,
  21. and <filename>C:\Program Files\Zend\ZendServer\share\ZendFramework</filename> on
  22. Windows. 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 the command <command>zf</command>, please substitute
  51. the absolute path to the script. On Unix-like systems, you may want to use your
  52. shell's alias functionality: <command>alias
  53. 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">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. % zf create project quickstart
  68. ]]></programlisting>
  69. <para>
  70. Running this command will create your basic site structure, including your initial
  71. controllers and views. The tree looks like the following:
  72. </para>
  73. <programlisting language="text"><![CDATA[
  74. quickstart
  75. |-- application
  76. | |-- Bootstrap.php
  77. | |-- configs
  78. | | `-- application.ini
  79. | |-- controllers
  80. | | |-- ErrorController.php
  81. | | `-- IndexController.php
  82. | |-- models
  83. | `-- views
  84. | |-- helpers
  85. | `-- scripts
  86. | |-- error
  87. | | `-- error.phtml
  88. | `-- index
  89. | `-- index.phtml
  90. |-- library
  91. |-- public
  92. | |-- .htaccess
  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 <acronym>PHP</acronym> environment (for instance, turning
  149. error reporting on and off), indicating the path to your bootstrap class (as well as its
  150. class name), and 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. appnamespace = "Application"
  161. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  162. resources.frontController.params.displayExceptions = 0
  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
  173. <acronym>INI</acronym>-style configuration, you can reference constants directly and
  174. expand them; <constant>APPLICATION_PATH</constant> is actually a constant. Additionally
  175. note that there are several sections defined: production, staging, testing, and
  176. development. The latter three inherit settings from the "production" environment. This
  177. is a useful way to organize configuration to ensure that appropriate settings are
  178. available in each stage 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
  198. <acronym>HTTP</acronym> 404 errors (controller or action not found) and
  199. <acronym>HTTP</acronym> 500 errors (application errors).
  200. </para>
  201. <para>
  202. The default <classname>IndexController</classname> is as follows:
  203. </para>
  204. <programlisting language="php"><![CDATA[
  205. // application/controllers/IndexController.php
  206. class IndexController extends Zend_Controller_Action
  207. {
  208. public function init()
  209. {
  210. /* Initialize action controller here */
  211. }
  212. public function indexAction()
  213. {
  214. // action body
  215. }
  216. }
  217. ]]></programlisting>
  218. <para>
  219. And the default <classname>ErrorController</classname> is as follows:
  220. </para>
  221. <programlisting language="php"><![CDATA[
  222. // application/controllers/ErrorController.php
  223. class ErrorController extends Zend_Controller_Action
  224. {
  225. public function errorAction()
  226. {
  227. $errors = $this->_getParam('error_handler');
  228. switch ($errors->type) {
  229. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  230. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  231. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  232. // 404 error -- controller or action not found
  233. $this->getResponse()->setHttpResponseCode(404);
  234. $this->view->message = 'Page not found';
  235. break;
  236. default:
  237. // application error
  238. $this->getResponse()->setHttpResponseCode(500);
  239. $this->view->message = 'Application error';
  240. break;
  241. }
  242. $this->view->exception = $errors->exception;
  243. $this->view->request = $errors->request;
  244. }
  245. }
  246. ]]></programlisting>
  247. <para>
  248. You'll note that (1) the <classname>IndexController</classname> contains no real code,
  249. and (2) the <classname>ErrorController</classname> makes reference to a "view" property.
  250. That leads nicely into our next subject.
  251. </para>
  252. </sect2>
  253. <sect2 id="learning.quickstart.create-project.views">
  254. <title>Views</title>
  255. <para>
  256. Views in Zend Framework are written in plain old <acronym>PHP</acronym>. View scripts
  257. are placed in <filename>application/views/scripts/</filename>, where they are further
  258. categorized using the controller names. In our case, we have an
  259. <classname>IndexController</classname> and an <classname>ErrorController</classname>,
  260. and thus we have corresponding <filename>index/</filename> and
  261. <filename>error/</filename> subdirectories within our view scripts directory. Within
  262. these subdirectories, you will then find and create view scripts that correspond to each
  263. controller action exposed; in the default case, we thus have the view scripts
  264. <filename>index/index.phtml</filename> and <filename>error/error.phtml</filename>.
  265. </para>
  266. <para>
  267. View scripts may contain any markup you want, and use the <emphasis>&lt;?php</emphasis>
  268. opening tag and <emphasis>?&gt;</emphasis> closing tag to insert <acronym>PHP</acronym>
  269. directives.
  270. </para>
  271. <para>
  272. The following is what we install by default for the
  273. <filename>index/index.phtml</filename> view script:
  274. </para>
  275. <programlisting language="php"><![CDATA[
  276. <!-- application/views/scripts/index/index.phtml -->
  277. <style>
  278. a:link,
  279. a:visited
  280. {
  281. color: #0398CA;
  282. }
  283. span#zf-name
  284. {
  285. color: #91BE3F;
  286. }
  287. div#welcome
  288. {
  289. color: #FFFFFF;
  290. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  291. width: 600px;
  292. height: 400px;
  293. border: 2px solid #444444;
  294. overflow: hidden;
  295. text-align: center;
  296. }
  297. div#more-information
  298. {
  299. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  300. height: 100%;
  301. }
  302. </style>
  303. <div id="welcome">
  304. <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />
  305. <h3>This is your project's main page<h3 />
  306. <div id="more-information">
  307. <p>
  308. <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />
  309. </p>
  310. <p>
  311. Helpful Links: <br />
  312. <a href="http://framework.zend.com/">Zend Framework Website</a> |
  313. <a href="http://framework.zend.com/manual/en/">Zend Framework
  314. Manual</a>
  315. </p>
  316. </div>
  317. </div>
  318. ]]></programlisting>
  319. <para>
  320. The <filename>error/error.phtml</filename> view script is slightly more interesting as
  321. it uses some <acronym>PHP</acronym> conditionals:
  322. </para>
  323. <programlisting language="php"><![CDATA[
  324. <!-- application/views/scripts/error/error.phtml -->
  325. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";
  326. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
  327. <html xmlns="http://www.w3.org/1999/xhtml">
  328. <head>
  329. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  330. <title>Zend Framework Default Application</title>
  331. </head>
  332. <body>
  333. <h1>An error occurred</h1>
  334. <h2><?php echo $this->message ?></h2>
  335. <?php if ('development' == $this->env): ?>
  336. <h3>Exception information:</h3>
  337. <p>
  338. <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  339. </p>
  340. <h3>Stack trace:</h3>
  341. <pre><?php echo $this->exception->getTraceAsString() ?>
  342. </pre>
  343. <h3>Request Parameters:</h3>
  344. <pre><?php echo var_export($this->request->getParams(), 1) ?>
  345. </pre>
  346. <?php endif ?>
  347. </body>
  348. </html>
  349. ]]></programlisting>
  350. </sect2>
  351. <sect2 id="learning.quickstart.create-project.vhost">
  352. <title>Create a virtual host</title>
  353. <para>
  354. For purposes of this quick start, we will assume you are using the <ulink
  355. url="http://httpd.apache.org/">Apache web server</ulink>. Zend Framework works
  356. perfectly well with other web servers -- including Microsoft Internet Information
  357. Server, lighttpd, nginx, and more -- but most developers should be famililar with Apache
  358. at the minimum, and it provides an easy introduction to Zend Framework's directory
  359. structure and rewrite capabilities.
  360. </para>
  361. <para>
  362. To create your vhost, you need to know the location of your
  363. <filename>httpd.conf</filename> file, and potentially where other configuration files
  364. are located. Some common locations:
  365. </para>
  366. <itemizedlist>
  367. <listitem>
  368. <para>
  369. <filename>/etc/httpd/httpd.conf</filename> (Fedora, RHEL, and others)
  370. </para>
  371. </listitem>
  372. <listitem>
  373. <para>
  374. <filename>/etc/apache2/httpd.conf</filename> (Debian, Ubuntu, and others)
  375. </para>
  376. </listitem>
  377. <listitem>
  378. <para>
  379. <filename>/usr/local/zend/etc/httpd.conf</filename> (Zend Server on *nix
  380. machines)
  381. </para>
  382. </listitem>
  383. <listitem>
  384. <para>
  385. <filename>C:\Program Files\Zend\Apache2\conf</filename> (Zend Server on Windows
  386. machines)
  387. </para>
  388. </listitem>
  389. </itemizedlist>
  390. <para>
  391. Within your <filename>httpd.conf</filename> (or <filename>httpd-vhosts.conf</filename>
  392. on some systems), you will need to do two things. First, ensure that the
  393. <varname>NameVirtualHost</varname> is defined; typically, you will set it to a value of
  394. "*:80". Second, define a virtual host:
  395. </para>
  396. <programlisting language="apache"><![CDATA[
  397. <VirtualHost *:80>
  398. ServerName quickstart.local
  399. DocumentRoot /path/to/quickstart/public
  400. SetEnv APPLICATION_ENV "development"
  401. <Directory /path/to/quickstart/public>
  402. DirectoryIndex index.php
  403. AllowOverride All
  404. Order allow,deny
  405. Allow from all
  406. </Directory>
  407. </VirtualHost>
  408. ]]></programlisting>
  409. <para>
  410. There are several things to note. First, note that the <varname>DocumentRoot</varname>
  411. setting specifies the <filename>public</filename> subdirectory of our project; this
  412. means that only files under that directory can ever be served directly by the server.
  413. Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
  414. <varname>Allow</varname> directives; these are to allow us to use
  415. <filename>htacess</filename> files within our project. During development, this is a
  416. good practice, as it prevents the need to constantly restart the web server as you make
  417. changes to your site directives; however, in production, you should likely push the
  418. content of your <filename>htaccess</filename> file into your server configuration and
  419. disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
  420. here is setting an environment variable for your virtual host; this variable will be
  421. picked up in the <filename>index.php</filename> and used to set the
  422. <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
  423. production, you can omit this directive (in which case it will default to the value
  424. "production") or set it explicitly to "production".
  425. </para>
  426. <para>
  427. Finally, you will need to add an entry in your <filename>hosts</filename> file
  428. corresponding to the value you place in your <varname>ServerName</varname> directive. On
  429. *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
  430. typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
  431. the system, the entry will look like the following:
  432. </para>
  433. <programlisting language="text"><![CDATA[
  434. 127.0.0.1 quickstart.local
  435. ]]></programlisting>
  436. <para>
  437. Start your webserver (or restart it), and you should be ready to go.
  438. </para>
  439. </sect2>
  440. <sect2 id="learning.quickstart.create-project.checkpoint">
  441. <title>Checkpoint</title>
  442. <para>
  443. At this point, you should be able to fire up your initial Zend Framework application.
  444. Point your browser to the server name you configured in the previous section; you should
  445. be able to see a welcome page at this point.
  446. </para>
  447. </sect2>
  448. </sect1>