project-structure.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 18056 -->
  3. <!-- Reviewed: no -->
  4. <appendix id="project-structure">
  5. <title>Recommended Project Structure for Zend Framework MVC Applications</title>
  6. <sect1 id="project-structure.overview">
  7. <title>Overview</title>
  8. <para>
  9. Many developers seek guidance on the best project structure for a Zend Framework project
  10. in a relatively flexible environment. A "flexible" environment is one in which the
  11. developer can manipulate their file systems and web server configurations as needed to
  12. achieve the most ideal project structure to run and secure their application. The
  13. default project structure will assume that the developer has such flexibility at their
  14. disposal.
  15. </para>
  16. <para>
  17. The following directory structure is designed to be maximally extensible for complex
  18. projects, while providing a simple subset of folder and files for project with simpler
  19. requirements. This structure also works without alteration for both modular and
  20. non-modular Zend Framework applications. The <filename>.htaccess</filename> files
  21. require <acronym>URL</acronym> rewrite functionality in the web server as described in
  22. the <link linkend="project-structure.rewrite">Rewrite Configuration Guide</link>, also
  23. included in this appendix.
  24. </para>
  25. <para>
  26. It is not the intention that this project structure will support all possible Zend
  27. Framework project requirements. The default project profile used by
  28. <classname>Zend_Tool</classname> reflect this project structure, but applications with
  29. requirements not supported by this structure should use a custom project profile.
  30. </para>
  31. </sect1>
  32. <sect1 id="project-structure.project">
  33. <title>Recommended Project Directory Structure</title>
  34. <programlisting language="txt"><![CDATA[
  35. <project name>/
  36. application/
  37. configs/
  38. application.ini
  39. controllers/
  40. helpers/
  41. forms/
  42. layouts/
  43. filters/
  44. helpers/
  45. scripts/
  46. models/
  47. modules/
  48. services/
  49. views/
  50. filters/
  51. helpers/
  52. scripts/
  53. Bootstrap.php
  54. data/
  55. cache/
  56. indexes/
  57. locales/
  58. logs/
  59. sessions/
  60. uploads/
  61. docs/
  62. library/
  63. public/
  64. css/
  65. images/
  66. js/
  67. .htaccess
  68. index.php
  69. scripts/
  70. jobs/
  71. build/
  72. temp/
  73. tests/
  74. ]]></programlisting>
  75. <para>
  76. The following describes the use cases for each directory as listed.
  77. </para>
  78. <itemizedlist>
  79. <listitem>
  80. <para>
  81. <emphasis><filename>application/</filename></emphasis>: This directory contains
  82. your application. It will house the <acronym>MVC</acronym> system, as well as
  83. configurations, services used, and your bootstrap file.
  84. </para>
  85. <itemizedlist>
  86. <listitem>
  87. <para>
  88. <emphasis><filename>configs/</filename></emphasis>: The
  89. application-wide configuration directory.
  90. </para>
  91. </listitem>
  92. <listitem>
  93. <para>
  94. <emphasis><filename>controllers/</filename></emphasis>,
  95. <emphasis><filename>models/</filename></emphasis>, and
  96. <emphasis><filename>views/</filename></emphasis>: These directories
  97. serve as the default controller, model or view directories. Having
  98. these three directories inside the application directory provides the
  99. best layout for starting a simple project as well as starting a modular
  100. project that has global <filename>controllers/models/views</filename>.
  101. </para>
  102. </listitem>
  103. <listitem>
  104. <para>
  105. <emphasis><filename>controllers/helpers/</filename></emphasis>: These
  106. directories will contain action helpers. Action helpers will be
  107. namespaced either as "<classname>Controller_Helper_</classname>" for
  108. the default module or "&lt;Module&gt;_Controller_Helper" in other
  109. modules.
  110. </para>
  111. </listitem>
  112. <listitem>
  113. <para>
  114. <emphasis><filename>layouts/</filename></emphasis>: This layout
  115. directory is for <acronym>MCV</acronym>-based layouts. Since
  116. <classname>Zend_Layout</classname> is capable of
  117. <acronym>MVC</acronym>- and non-<acronym>MVC</acronym>-based layouts,
  118. the location of this directory reflects that layouts are not on a
  119. 1-to-1 relationship with controllers and are independent of templates
  120. within <filename>views/</filename>.
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <emphasis><filename>modules/</filename></emphasis>: Modules allow a
  126. developer to group a set of related controllers into a logically
  127. organized group. The structure under the modules directory would
  128. resemble the structure under the application directory.
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. <emphasis><filename>services/</filename></emphasis>: This directory is
  134. for your application specific web-service files that are provided by
  135. your application, or for implementing a <ulink
  136. url="http://www.martinfowler.com/eaaCatalog/serviceLayer.html">Service
  137. Layer</ulink> for your models.
  138. </para>
  139. </listitem>
  140. <listitem>
  141. <para>
  142. <emphasis><filename>Bootstrap.php</filename></emphasis>: This file is
  143. the entry point for your application, and should implement
  144. <classname>Zend_Application_Bootstrap_Bootstrapper</classname>.
  145. The purpose for this file is to bootstrap the application and make
  146. components available to the application by initializing them.
  147. </para>
  148. </listitem>
  149. </itemizedlist>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. <emphasis><filename>data/</filename></emphasis>: This directory provides a
  154. place to store application data that is volatile and possibly temporary. The
  155. disturbance of data in this directory might cause the application to fail.
  156. Also, the information in this directory may or may not be committed to a
  157. subversion repository. Examples of things in this directory are session files,
  158. cache files, sqlite databases, logs and indexes.
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <emphasis><filename>docs/</filename></emphasis>: This directory contains
  164. documentation, either generated or directly authored.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <emphasis><filename>library/</filename></emphasis>: This directory is for
  170. common libraries on which the application depends, and should be on the
  171. <acronym>PHP</acronym> <property>include_path</property>. Developers should
  172. place their application's library code under this directory in a unique
  173. namespace, following the guidelines established in the <acronym>PHP</acronym>
  174. manual's <ulink
  175. url="http://www.php.net/manual/en/userlandnaming.php">Userland Naming
  176. Guide</ulink>, as well as those established by Zend itself. This
  177. directory may also include Zend Framework itself; if so, you would house it in
  178. <filename>library/Zend/</filename>.
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <emphasis><filename>public/</filename></emphasis>: This directory contains all
  184. public files for your application. <filename>index.php</filename> sets up and
  185. invokes <classname>Zend_Application</classname>, which in turn invokes the
  186. <filename>application/Bootstrap.php</filename> file, resulting in dispatching
  187. the front controller. The web root of your web server would typically be set to
  188. this directory.
  189. </para>
  190. </listitem>
  191. <listitem>
  192. <para>
  193. <emphasis><filename>scripts/</filename></emphasis>: This directory contains
  194. maintenance and/or build scripts. Such scripts might include command line,
  195. cron, or phing build scripts that are not executed at runtime but are part of
  196. the correct functioning of the application.
  197. </para>
  198. </listitem>
  199. <listitem>
  200. <para>
  201. <emphasis><filename>temp/</filename></emphasis>: The <filename>temp/</filename>
  202. folder is set aside for transient application data. This information would not
  203. typically be committed to the applications svn repository. If data under the
  204. <filename>temp/</filename> directory were deleted, the application should be
  205. able to continue running with a possible decrease in performance until data is
  206. once again restored or recached.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. <emphasis><filename>tests/</filename></emphasis>: This directory contains
  212. application tests. These could be hand-written, PHPUnit tests, Selenium-RC
  213. based tests or based on some other testing framework. By default, library code
  214. can be tested by mimicing the directory structure of your
  215. <filename>library/</filename> directory. Additionally, functional tests for
  216. your application could be written mimicing the
  217. <filename>application/</filename> directory structure (including the
  218. application subdirectory).
  219. </para>
  220. </listitem>
  221. </itemizedlist>
  222. </sect1>
  223. <sect1 id="project-structure.filesystem">
  224. <title>Module Structure</title>
  225. <para>
  226. The directory structure for modules should mimic that of the
  227. <filename>application/</filename> directory in the recommended project structure:
  228. </para>
  229. <programlisting language="xml"><![CDATA[
  230. <modulename>/
  231. configs/
  232. application.ini
  233. controllers/
  234. helpers/
  235. forms/
  236. layouts/
  237. filters/
  238. helpers/
  239. scripts/
  240. models/
  241. services/
  242. views/
  243. filters/
  244. helpers/
  245. scripts/
  246. Bootstrap.php
  247. ]]></programlisting>
  248. <para>
  249. The purpose of these directories remains exactly the same as for the recommended
  250. project directory structure.
  251. </para>
  252. </sect1>
  253. <sect1 id="project-structure.rewrite">
  254. <title>Rewrite Configuration Guide</title>
  255. <para>
  256. <acronym>URL</acronym> rewriting is a common function of <acronym>HTTP</acronym>
  257. servers. However, the rules and configuration differ widely between them. Below are
  258. some common approaches across a variety of popular web servers available at the time of
  259. writing.
  260. </para>
  261. <sect2 id="project-structure.rewrite.apache">
  262. <title>Apache HTTP Server</title>
  263. <para>
  264. All examples that follow use <property>mod_rewrite</property>, an official
  265. module that comes bundled with Apache. To use it,
  266. <property>mod_rewrite</property> must either be included at compile time or
  267. enabled as a Dynamic Shared Object (<acronym>DSO</acronym>). Please consult the
  268. <ulink url="http://httpd.apache.org/docs/">Apache documentation</ulink> for your
  269. version for more information.
  270. </para>
  271. <sect3 id="project-structure.rewrite.apache.vhost">
  272. <title>Rewriting inside a VirtualHost</title>
  273. <para>
  274. Here is a very basic virtual host definition. These rules direct all requests
  275. to <filename>index.php</filename>, except when a matching file is found under
  276. the <property>document_root</property>.
  277. </para>
  278. <programlisting language="xml"><![CDATA[
  279. <VirtualHost my.domain.com:80>
  280. ServerName my.domain.com
  281. DocumentRoot /path/to/server/root/my.domain.com/public
  282. RewriteEngine off
  283. <Location />
  284. RewriteEngine On
  285. RewriteCond %{REQUEST_FILENAME} -s [OR]
  286. RewriteCond %{REQUEST_FILENAME} -l [OR]
  287. RewriteCond %{REQUEST_FILENAME} -d
  288. RewriteRule ^.*$ - [NC,L]
  289. RewriteRule ^.*$ /index.php [NC,L]
  290. </Location>
  291. </VirtualHost>
  292. ]]></programlisting>
  293. <para>
  294. Note the slash ("/") prefixing <filename>index.php</filename>; the rules for
  295. <filename>.htaccess</filename> differ in this regard.
  296. </para>
  297. </sect3>
  298. <sect3 id="project-structure.rewrite.apache.htaccess">
  299. <title>Rewriting within a .htaccess file</title>
  300. <para>
  301. Below is a sample <filename>.htaccess</filename> file that utilizes
  302. <property>mod_rewrite</property>. It is similar to the virtual host
  303. configuration, except that it specifies only the rewrite rules, and the leading
  304. slash is omitted from <filename>index.php</filename>.
  305. </para>
  306. <programlisting language="text"><![CDATA[
  307. RewriteEngine On
  308. RewriteCond %{REQUEST_FILENAME} -s [OR]
  309. RewriteCond %{REQUEST_FILENAME} -l [OR]
  310. RewriteCond %{REQUEST_FILENAME} -d
  311. RewriteRule ^.*$ - [NC,L]
  312. RewriteRule ^.*$ index.php [NC,L]
  313. ]]></programlisting>
  314. <para>
  315. There are many ways to configure <property>mod_rewrite</property>; if you
  316. would like more information, see Jayson Minard's <ulink
  317. url="http://devzone.zend.com/a/70">Blueprint for PHP Applications:
  318. Bootstrapping</ulink>.
  319. </para>
  320. </sect3>
  321. </sect2>
  322. <sect2 id="project-structure.rewrite.iis">
  323. <title>Microsoft Internet Information Server</title>
  324. <para>
  325. As of version 7.0, <acronym>IIS</acronym> now ships with a standard rewrite engine.
  326. You may use the following configuration to create the appropriate rewrite rules.
  327. </para>
  328. <programlisting language="xml"><![CDATA[
  329. <?xml version="1.0" encoding="UTF-8"?>
  330. <configuration>
  331. <system.webServer>
  332. <rewrite>
  333. <rules>
  334. <rule name="Imported Rule 1" stopProcessing="true">
  335. <match url="^.*$" />
  336. <conditions logicalGrouping="MatchAny">
  337. <add input="{REQUEST_FILENAME}"
  338. matchType="IsFile" pattern=""
  339. ignoreCase="false" />
  340. <add input="{REQUEST_FILENAME}"
  341. matchType="IsDirectory"
  342. pattern=""
  343. ignoreCase="false" />
  344. </conditions>
  345. <action type="None" />
  346. </rule>
  347. <rule name="Imported Rule 2" stopProcessing="true">
  348. <match url="^.*$" />
  349. <action type="Rewrite" url="index.php" />
  350. </rule>
  351. </rules>
  352. </rewrite>
  353. </system.webServer>
  354. </configuration>
  355. ]]></programlisting>
  356. </sect2>
  357. </sect1>
  358. </appendix>