quickstart-create-project.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  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. <para>
  46. Zend Framework インストール先には <filename>bin/</filename> サブディレクトリがあり、
  47. UNIX ベースおよび Windows ベースのユーザーのために、それぞれスクリプト
  48. <filename>zf.sh</filename> および <filename>zf.bat</filename> を含みます。
  49. このスクリプトに絶対パスを書きとめてください。
  50. </para>
  51. <para>
  52. <command>zf</command> コマンドとの関係がわかっている場所ではどこでも
  53. スクリプトへの絶対パスに置き換えてください。Unix のようなシステムでは、シェルのエイリアス機能を使いたいかもしれません。
  54. <command>alias zf.sh=path/to/ZendFramework/bin/zf.sh</command>
  55. </para>
  56. <para>
  57. <command>zf</command> コマンドラインツール設定中に問題があった場合は、
  58. <link linkend="zend.tool.framework.clitool">マニュアル</link> を参照してください。
  59. </para>
  60. </note>
  61. <para>
  62. ターミナルを開きます。(Windows では<command>スタート -> ファイル名を指定して実行</command>
  63. 、それから <command>cmd</command> を使用します)
  64. プロジェクトを開始したいディレクトリに進みます。それから、適切なスクリプトへのパスを使用して、下記のいずれかを実行します。
  65. </para>
  66. <programlisting language="shell"><![CDATA[
  67. % zf create project quickstart
  68. ]]></programlisting>
  69. <para>
  70. このコマンドを実行すると、基本的なサイト構造が作成されます。そこには基になるコントローラやビューが含まれます。
  71. ツリーは下記のように見えます。
  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. もしこの時点で Zend Framework を <constant>include_path</constant>
  103. に追加していないなら、<filename>library/</filename> ディレクトリにコピーするか、または symlink することを勧めます。
  104. いずれにせよ、Zend Framework インストール先の <filename>library/Zend/</filename>
  105. ディレクトリをプロジェクトの <filename>library/</filename> ディレクトリに再帰的にコピーするか、
  106. または symlink することが必要になるでしょう。Unix のようなシステムでは、それは下記のいずれかのように見えます。
  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. Windows システムでは、これをエクスプローラから行なうのが最も簡単かもしれません。
  116. </para>
  117. <!-- TODO : to be translated -->
  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). そのクラスは下記のように見えます。
  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. それは下記のようになります。
  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. デフォルトの <classname>IndexController</classname> は下記の通りです。
  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. そして、デフォルトの <classname>ErrorController</classname> は下記の通りです。
  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>バーチャル・ホストを作成</title>
  353. <para>
  354. このクイックスタートの必要上、<ulink url="http://httpd.apache.org/">Apache Web サーバー</ulink>
  355. を使用すると仮定しています。Zend Framework は、他の Web サーバーでも申し分なく動作します。
  356. それには、Microsoft Internet Information Server、lighttpd、nginx などを含みます。
  357. しかし、大抵の開発者は少なくとも Apache をよく知っていなければなりません。
  358. そして、それはZend Framework のディレクトリ構造とリライト能力への簡単な導入を提示します。
  359. </para>
  360. <para>
  361. vhost を作成するには、<filename>httpd.conf</filename> ファイルの位置を知る必要があります。
  362. その場所には他の構成ファイルが格納されている可能性があります。いくつかの一般的な場所はこうです。
  363. </para>
  364. <itemizedlist>
  365. <listitem>
  366. <para>
  367. <filename>/etc/httpd/httpd.conf</filename> (Fedora や RHEL など)
  368. </para>
  369. </listitem>
  370. <listitem>
  371. <para>
  372. <filename>/etc/apache2/httpd.conf</filename> (Debian や Ubuntu など)
  373. </para>
  374. </listitem>
  375. <listitem>
  376. <para>
  377. <filename>/usr/local/zend/etc/httpd.conf</filename> (Linux 版 Zend Server)
  378. </para>
  379. </listitem>
  380. <listitem>
  381. <para>
  382. <filename>C:\Program Files\Zend\Apache2\conf</filename> (Windows 版 Zend Server)
  383. </para>
  384. </listitem>
  385. </itemizedlist>
  386. <para>
  387. <filename>httpd.conf</filename> (一部のシステムでは <filename>httpd-vhosts.conf</filename>)
  388. 内で行なうべきことが2つあります。
  389. 1つ目は、<varname>NameVirtualHost</varname> が定義されていることを確実にすることです。
  390. 一般的にはそれを "*:80" という値に設定します。
  391. 2つ目は、バーチャル・ホストを定義することです。
  392. </para>
  393. <programlisting language="apache"><![CDATA[
  394. <VirtualHost *:80>
  395. ServerName quickstart.local
  396. DocumentRoot /path/to/quickstart/public
  397. SetEnv APPLICATION_ENV "development"
  398. <Directory /path/to/quickstart/public>
  399. DirectoryIndex index.php
  400. AllowOverride All
  401. Order allow,deny
  402. Allow from all
  403. </Directory>
  404. </VirtualHost>
  405. ]]></programlisting>
  406. <!-- TODO : to be translated -->
  407. <para>
  408. There are several things to note. First, note that the <varname>DocumentRoot</varname>
  409. setting specifies the <filename>public</filename> subdirectory of our project; this
  410. means that only files under that directory can ever be served directly by the server.
  411. Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
  412. <varname>Allow</varname> directives; these are to allow us to use
  413. <filename>htacess</filename> files within our project. During development, this is a
  414. good practice, as it prevents the need to constantly restart the web server as you make
  415. changes to your site directives; however, in production, you should likely push the
  416. content of your <filename>htaccess</filename> file into your server configuration and
  417. disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
  418. here is setting an environment variable for your virtual host; this variable will be
  419. picked up in the <filename>index.php</filename> and used to set the
  420. <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
  421. production, you can omit this directive (in which case it will default to the value
  422. "production") or set it explicitly to "production".
  423. </para>
  424. <para>
  425. Finally, you will need to add an entry in your <filename>hosts</filename> file
  426. corresponding to the value you place in your <varname>ServerName</varname> directive. On
  427. *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
  428. typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
  429. the system, the entry will look like the following:
  430. </para>
  431. <programlisting language="text"><![CDATA[
  432. 127.0.0.1 quickstart.local
  433. ]]></programlisting>
  434. <para>
  435. Start your webserver (or restart it), and you should be ready to go.
  436. </para>
  437. </sect2>
  438. <sect2 id="learning.quickstart.create-project.checkpoint">
  439. <title>Checkpoint</title>
  440. <para>
  441. At this point, you should be able to fire up your initial Zend Framework application.
  442. Point your browser to the server name you configured in the previous section; you should
  443. be able to see a welcome page at this point.
  444. </para>
  445. </sect2>
  446. </sect1>