project-structure.xml 16 KB

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