quickstart-create-project.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 21997 -->
  4. <sect1 id="learning.quickstart.create-project">
  5. <title>プロジェクトを作成</title>
  6. <para>
  7. プロジェクトを作成するには、まず、Zend Frameworkをダウンロードして、解凍しなければいけません。
  8. </para>
  9. <sect2 id="learning.quickstart.create-project.install-zf">
  10. <title>Zend Framework をインストール</title>
  11. <para>
  12. 全部揃った <acronym>PHP</acronym> スタックと一緒に Zend Framework
  13. を手に入れる最も簡単な方法は、<ulink
  14. url="http://www.zend.co.jp/product/zendserver.html">Zend Server</ulink>
  15. をインストールすることです。Zend Server には、
  16. 多くの Linux ディストリビューションと互換性のある一般的なインストール・パッケージだけでなく、
  17. Mac OSX、Windows、Fedora Core および Ubuntu 用のネイティブのインストーラーがあります。
  18. </para>
  19. <para>
  20. Zend Server をインストールし終わると、Mac OSX および Linux の場合は
  21. <filename>/usr/local/zend/share/ZendFramework</filename> 配下に、
  22. Windows の場合は
  23. <filename>C:\Program Files\Zend\ZendServer\share\ZendFramework</filename>
  24. 配下にフレームワークのファイルが見つかるでしょう。
  25. <constant>include_path</constant>は、Zend Frameworkを含むように構成済みです。
  26. </para>
  27. <para>
  28. あるいは、<ulink
  29. url="http://framework.zend.com/download/latest">Zend Framework の最新版</ulink>
  30. をダウンロードして内容を解凍することができます。
  31. <!-- TODO -->make a note of where you have done so.
  32. </para>
  33. <para>
  34. <filename>php.ini</filename> の <constant>include_path</constant> 設定に
  35. アーカイブの<filename>library/</filename>サブディレクトリへのパスを任意に追加できます。
  36. </para>
  37. <para>
  38. これで終わりです。これで Zend Framework がインストールされ、使えるようになりました。
  39. </para>
  40. </sect2>
  41. <sect2 id="learning.quickstart.create-project.create-project">
  42. <title>プロジェクトを作成</title>
  43. <note>
  44. <title>zf コマンドラインツール</title>
  45. <!-- TODO : to be translated -->
  46. <para>
  47. In your Zend Framework installation is a <filename>bin/</filename> subdirectory,
  48. containing the scripts <filename>zf.sh</filename> and <filename>zf.bat</filename>
  49. for Unix-based and Windows-based users, respectively. Make a note of the absolute
  50. path to this script.
  51. </para>
  52. <para>
  53. Wherever you see references to the command <command>zf</command>, please substitute
  54. the absolute path to the script. On Unix-like systems, you may want to use your
  55. shell's alias functionality: <command>alias
  56. zf.sh=path/to/ZendFramework/bin/zf.sh</command>.
  57. </para>
  58. <para>
  59. If you have problems setting up the <command>zf</command> command-line tool, please
  60. refer to <link linkend="zend.tool.framework.clitool">the
  61. manual</link>.
  62. </para>
  63. </note>
  64. <para>
  65. Open a terminal (in Windows, <command>Start -> Run</command>, and then use
  66. <command>cmd</command>). Navigate to a directory where you would like to start a
  67. project. Then, use the path to the appropriate script, and execute one of the following:
  68. </para>
  69. <programlisting language="shell"><![CDATA[
  70. % zf 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. | |-- .htaccess
  96. | `-- index.php
  97. `-- tests
  98. |-- application
  99. | `-- bootstrap.php
  100. |-- library
  101. | `-- bootstrap.php
  102. `-- phpunit.xml
  103. ]]></programlisting>
  104. <para>
  105. At this point, if you haven't added Zend Framework to your
  106. <constant>include_path</constant>, we recommend either copying or symlinking it into
  107. your <filename>library/</filename> directory. In either case, you'll want to either
  108. recursively copy or symlink the <filename>library/Zend/</filename> directory of your
  109. Zend Framework installation into the <filename>library/</filename> directory of your
  110. project. On unix-like systems, that would look like one of the following:
  111. </para>
  112. <programlisting language="shell"><![CDATA[
  113. # Symlink:
  114. % cd library; ln -s path/to/ZendFramework/library/Zend .
  115. # Copy:
  116. % cd library; cp -r path/to/ZendFramework/library/Zend .
  117. ]]></programlisting>
  118. <para>
  119. On Windows systems, it may be easiest to do this from the Explorer.
  120. </para>
  121. <para>
  122. Now that the project is created, the main artifacts to begin understanding are the
  123. bootstrap, configuration, action controllers, and views.
  124. </para>
  125. </sect2>
  126. <sect2 id="learning.quickstart.create-project.bootstrap">
  127. <title>The Bootstrap</title>
  128. <para>
  129. Your <classname>Bootstrap</classname> class defines what resources and components to
  130. initialize. By default, Zend Framework's <link linkend="zend.controller.front">Front
  131. Controller</link> is initialized, and it uses the
  132. <filename>application/controllers/</filename> as the default directory in which to look
  133. for action controllers (more on that later). The class looks like the following:
  134. </para>
  135. <programlisting language="php"><![CDATA[
  136. // application/Bootstrap.php
  137. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  138. {
  139. }
  140. ]]></programlisting>
  141. <para>
  142. As you can see, not much is necessary to begin with.
  143. </para>
  144. </sect2>
  145. <sect2 id="learning.quickstart.create-project.configuration">
  146. <title>Configuration</title>
  147. <para>
  148. While Zend Framework is itself configurationless, you often need to configure your
  149. application. The default configuration is placed in
  150. <filename>application/configs/application.ini</filename>, and contains some basic
  151. directives for setting your <acronym>PHP</acronym> environment (for instance, turning
  152. error reporting on and off), indicating the path to your bootstrap class (as well as its
  153. class name), and the path to your action controllers. It looks as follows:
  154. </para>
  155. <programlisting language="ini"><![CDATA[
  156. ; application/configs/application.ini
  157. [production]
  158. phpSettings.display_startup_errors = 0
  159. phpSettings.display_errors = 0
  160. includePaths.library = APPLICATION_PATH "/../library"
  161. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  162. bootstrap.class = "Bootstrap"
  163. appnamespace = "Application"
  164. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  165. resources.frontController.params.displayExceptions = 0
  166. [staging : production]
  167. [testing : production]
  168. phpSettings.display_startup_errors = 1
  169. phpSettings.display_errors = 1
  170. [development : production]
  171. phpSettings.display_startup_errors = 1
  172. phpSettings.display_errors = 1
  173. ]]></programlisting>
  174. <para>
  175. Several things about this file should be noted. First, when using
  176. <acronym>INI</acronym>-style configuration, you can reference constants directly and
  177. expand them; <constant>APPLICATION_PATH</constant> is actually a constant. Additionally
  178. note that there are several sections defined: production, staging, testing, and
  179. development. The latter three inherit settings from the "production" environment. This
  180. is a useful way to organize configuration to ensure that appropriate settings are
  181. available in each stage of application development.
  182. </para>
  183. </sect2>
  184. <sect2 id="learning.quickstart.create-project.action-controllers">
  185. <title>Action Controllers</title>
  186. <para>
  187. Your application's <emphasis>action controllers</emphasis> contain your application
  188. workflow, and do the work of mapping your requests to the appropriate models and views.
  189. </para>
  190. <para>
  191. An action controller should have one or more methods ending in "Action"; these methods
  192. may then be requested via the web. By default, Zend Framework URLs follow the schema
  193. <constant>/controller/action</constant>, where "controller" maps to the action
  194. controller name (minus the "Controller" suffix) and "action" maps to an action method
  195. (minus the "Action" suffix).
  196. </para>
  197. <para>
  198. Typically, you always need an <classname>IndexController</classname>, which is a
  199. fallback controller and which also serves the home page of the site, and an
  200. <classname>ErrorController</classname>, which is used to indicate things such as
  201. <acronym>HTTP</acronym> 404 errors (controller or action not found) and
  202. <acronym>HTTP</acronym> 500 errors (application errors).
  203. </para>
  204. <para>
  205. The default <classname>IndexController</classname> is as follows:
  206. </para>
  207. <programlisting language="php"><![CDATA[
  208. // application/controllers/IndexController.php
  209. class IndexController extends Zend_Controller_Action
  210. {
  211. public function init()
  212. {
  213. /* Initialize action controller here */
  214. }
  215. public function indexAction()
  216. {
  217. // action body
  218. }
  219. }
  220. ]]></programlisting>
  221. <para>
  222. And the default <classname>ErrorController</classname> is as follows:
  223. </para>
  224. <programlisting language="php"><![CDATA[
  225. // application/controllers/ErrorController.php
  226. class ErrorController extends Zend_Controller_Action
  227. {
  228. public function errorAction()
  229. {
  230. $errors = $this->_getParam('error_handler');
  231. switch ($errors->type) {
  232. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  233. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  234. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  235. // 404 error -- controller or action not found
  236. $this->getResponse()->setHttpResponseCode(404);
  237. $this->view->message = 'Page not found';
  238. break;
  239. default:
  240. // application error
  241. $this->getResponse()->setHttpResponseCode(500);
  242. $this->view->message = 'Application error';
  243. break;
  244. }
  245. $this->view->exception = $errors->exception;
  246. $this->view->request = $errors->request;
  247. }
  248. }
  249. ]]></programlisting>
  250. <para>
  251. You'll note that (1) the <classname>IndexController</classname> contains no real code,
  252. and (2) the <classname>ErrorController</classname> makes reference to a "view" property.
  253. That leads nicely into our next subject.
  254. </para>
  255. </sect2>
  256. <sect2 id="learning.quickstart.create-project.views">
  257. <title>Views</title>
  258. <para>
  259. Views in Zend Framework are written in plain old <acronym>PHP</acronym>. View scripts
  260. are placed in <filename>application/views/scripts/</filename>, where they are further
  261. categorized using the controller names. In our case, we have an
  262. <classname>IndexController</classname> and an <classname>ErrorController</classname>,
  263. and thus we have corresponding <filename>index/</filename> and
  264. <filename>error/</filename> subdirectories within our view scripts directory. Within
  265. these subdirectories, you will then find and create view scripts that correspond to each
  266. controller action exposed; in the default case, we thus have the view scripts
  267. <filename>index/index.phtml</filename> and <filename>error/error.phtml</filename>.
  268. </para>
  269. <para>
  270. View scripts may contain any markup you want, and use the <emphasis>&lt;?php</emphasis>
  271. opening tag and <emphasis>?&gt;</emphasis> closing tag to insert <acronym>PHP</acronym>
  272. directives.
  273. </para>
  274. <para>
  275. The following is what we install by default for the
  276. <filename>index/index.phtml</filename> view script:
  277. </para>
  278. <programlisting language="php"><![CDATA[
  279. <!-- application/views/scripts/index/index.phtml -->
  280. <style>
  281. a:link,
  282. a:visited
  283. {
  284. color: #0398CA;
  285. }
  286. span#zf-name
  287. {
  288. color: #91BE3F;
  289. }
  290. div#welcome
  291. {
  292. color: #FFFFFF;
  293. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  294. width: 600px;
  295. height: 400px;
  296. border: 2px solid #444444;
  297. overflow: hidden;
  298. text-align: center;
  299. }
  300. div#more-information
  301. {
  302. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  303. height: 100%;
  304. }
  305. </style>
  306. <div id="welcome">
  307. <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />
  308. <h3>This is your project's main page<h3 />
  309. <div id="more-information">
  310. <p>
  311. <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />
  312. </p>
  313. <p>
  314. Helpful Links: <br />
  315. <a href="http://framework.zend.com/">Zend Framework Website</a> |
  316. <a href="http://framework.zend.com/manual/en/">Zend Framework
  317. Manual</a>
  318. </p>
  319. </div>
  320. </div>
  321. ]]></programlisting>
  322. <para>
  323. The <filename>error/error.phtml</filename> view script is slightly more interesting as
  324. it uses some <acronym>PHP</acronym> conditionals:
  325. </para>
  326. <programlisting language="php"><![CDATA[
  327. <!-- application/views/scripts/error/error.phtml -->
  328. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";
  329. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
  330. <html xmlns="http://www.w3.org/1999/xhtml">
  331. <head>
  332. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  333. <title>Zend Framework Default Application</title>
  334. </head>
  335. <body>
  336. <h1>An error occurred</h1>
  337. <h2><?php echo $this->message ?></h2>
  338. <?php if ('development' == $this->env): ?>
  339. <h3>Exception information:</h3>
  340. <p>
  341. <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  342. </p>
  343. <h3>Stack trace:</h3>
  344. <pre><?php echo $this->exception->getTraceAsString() ?>
  345. </pre>
  346. <h3>Request Parameters:</h3>
  347. <pre><?php echo var_export($this->request->getParams(), 1) ?>
  348. </pre>
  349. <?php endif ?>
  350. </body>
  351. </html>
  352. ]]></programlisting>
  353. </sect2>
  354. <sect2 id="learning.quickstart.create-project.vhost">
  355. <title>Create a virtual host</title>
  356. <para>
  357. For purposes of this quick start, we will assume you are using the <ulink
  358. url="http://httpd.apache.org/">Apache web server</ulink>. Zend Framework works
  359. perfectly well with other web servers -- including Microsoft Internet Information
  360. Server, lighttpd, nginx, and more -- but most developers should be famililar with Apache
  361. at the minimum, and it provides an easy introduction to Zend Framework's directory
  362. structure and rewrite capabilities.
  363. </para>
  364. <para>
  365. To create your vhost, you need to know the location of your
  366. <filename>httpd.conf</filename> file, and potentially where other configuration files
  367. are located. Some common locations:
  368. </para>
  369. <itemizedlist>
  370. <listitem>
  371. <para>
  372. <filename>/etc/httpd/httpd.conf</filename> (Fedora や RHEL など)
  373. </para>
  374. </listitem>
  375. <listitem>
  376. <para>
  377. <filename>/etc/apache2/httpd.conf</filename> (Debian や Ubuntu など)
  378. </para>
  379. </listitem>
  380. <listitem>
  381. <para>
  382. <filename>/usr/local/zend/etc/httpd.conf</filename> (Linux 版 Zend Server)
  383. </para>
  384. </listitem>
  385. <listitem>
  386. <para>
  387. <filename>C:\Program Files\Zend\Apache2\conf</filename> (Windows 版 Zend Server)
  388. </para>
  389. </listitem>
  390. </itemizedlist>
  391. <!-- TODO : to be translated -->
  392. <para>
  393. Within your <filename>httpd.conf</filename> (or <filename>httpd-vhosts.conf</filename>
  394. on some systems), you will need to do two things. First, ensure that the
  395. <varname>NameVirtualHost</varname> is defined; typically, you will set it to a value of
  396. "*:80". Second, define a virtual host:
  397. </para>
  398. <programlisting language="apache"><![CDATA[
  399. <VirtualHost *:80>
  400. ServerName quickstart.local
  401. DocumentRoot /path/to/quickstart/public
  402. SetEnv APPLICATION_ENV "development"
  403. <Directory /path/to/quickstart/public>
  404. DirectoryIndex index.php
  405. AllowOverride All
  406. Order allow,deny
  407. Allow from all
  408. </Directory>
  409. </VirtualHost>
  410. ]]></programlisting>
  411. <para>
  412. There are several things to note. First, note that the <varname>DocumentRoot</varname>
  413. setting specifies the <filename>public</filename> subdirectory of our project; this
  414. means that only files under that directory can ever be served directly by the server.
  415. Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
  416. <varname>Allow</varname> directives; these are to allow us to use
  417. <filename>htacess</filename> files within our project. During development, this is a
  418. good practice, as it prevents the need to constantly restart the web server as you make
  419. changes to your site directives; however, in production, you should likely push the
  420. content of your <filename>htaccess</filename> file into your server configuration and
  421. disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
  422. here is setting an environment variable for your virtual host; this variable will be
  423. picked up in the <filename>index.php</filename> and used to set the
  424. <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
  425. production, you can omit this directive (in which case it will default to the value
  426. "production") or set it explicitly to "production".
  427. </para>
  428. <para>
  429. Finally, you will need to add an entry in your <filename>hosts</filename> file
  430. corresponding to the value you place in your <varname>ServerName</varname> directive. On
  431. *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
  432. typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
  433. the system, the entry will look like the following:
  434. </para>
  435. <programlisting language="text"><![CDATA[
  436. 127.0.0.1 quickstart.local
  437. ]]></programlisting>
  438. <para>
  439. Start your webserver (or restart it), and you should be ready to go.
  440. </para>
  441. </sect2>
  442. <sect2 id="learning.quickstart.create-project.checkpoint">
  443. <title>Checkpoint</title>
  444. <para>
  445. At this point, you should be able to fire up your initial Zend Framework application.
  446. Point your browser to the server name you configured in the previous section; you should
  447. be able to see a welcome page at this point.
  448. </para>
  449. </sect2>
  450. </sect1>