project-structure.xml 16 KB

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