project-structure.xml 17 KB

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