project-structure.xml 17 KB

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