autoloading-resources.xml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.autoloading.resources">
  4. <title>Resource Autoloading</title>
  5. <para>
  6. Often, when developing an application, it's either difficult to package classes in the 1:1
  7. classname:filename standard Zend Framework recommends, or it's advantageous for purposes of
  8. packaging not to do so. However, this means you class files will not be found by the
  9. autoloader.
  10. </para>
  11. <para>
  12. If you read through <link linkend="learning.autoloading.design">the design goals</link> for
  13. the autoloader, the last point in that section indicated that the solution should cover this
  14. situation. Zend Framework does so with
  15. <classname>Zend_Loader_Autoloader_Resource</classname>.
  16. </para>
  17. <para>
  18. A resource is just a name that corresponds to a component namespace (which
  19. is appended to the autoloader's namespace) and a path (which is relative to
  20. the autoloader's base path). In action, you'd do something like this:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. $loader = new Zend_Application_Module_Autoloader(array(
  24. 'namespace' => 'Blog',
  25. 'basePath' => APPLICATION_PATH . '/modules/blog',
  26. ));
  27. ]]></programlisting>
  28. <para>
  29. Once you have the loader in place, you then need to inform it of the various resource types
  30. it's aware of. These resource types are simply pairs of subtree and prefix.
  31. </para>
  32. <para>
  33. As an example, consider the following tree:
  34. </para>
  35. <programlisting language="text"><![CDATA[
  36. path/to/some/resources/
  37. |-- forms/
  38. | `-- Guestbook.php // Foo_Form_Guestbook
  39. |-- models/
  40. | |-- DbTable/
  41. | | `-- Guestbook.php // Foo_Model_DbTable_Guestbook
  42. | |-- Guestbook.php // Foo_Model_Guestbook
  43. | `-- GuestbookMapper.php // Foo_Model_GuestbookMapper
  44. ]]></programlisting>
  45. <para>
  46. Our first step is creating the resource loader:
  47. </para>
  48. <programlisting language="php"><![CDATA[
  49. $loader = new Zend_Loader_Autoloader_Resource(array(
  50. 'basePath' => 'path/to/some/resources/',
  51. 'namespace' => 'Foo',
  52. ));
  53. ]]></programlisting>
  54. <para>
  55. Next, we need to define some resource types.
  56. <methodname>Zend_Loader_Autoloader_Resourse::addResourceType()</methodname> has three
  57. arguments: the "type" of resource (an arbitrary string), the path under the base path in
  58. which the resource type may be found, and the component prefix to use for the resource type.
  59. In the above tree, we have three resource types: form (in the subdirectory "forms", with a
  60. component prefix of "Form"), model (in the subdirectory "models", with a component prefix of
  61. "Model"), and dbtable (in the subdirectory "<filename>models/DbTable</filename>",
  62. with a component prefix of "<classname>Model_DbTable</classname>"). We'd define them as
  63. follows:
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. $loader->addResourceType('form', 'forms', 'Form')
  67. ->addResourceType('model', 'models', 'Model')
  68. ->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');
  69. ]]></programlisting>
  70. <para>
  71. Once defined, we can simply use these classes:
  72. </para>
  73. <programlisting language="php"><![CDATA[
  74. $form = new Foo_Form_Guestbook();
  75. $guestbook = new Foo_Model_Guestbook();
  76. ]]></programlisting>
  77. <note>
  78. <title>Module Resource Autoloading</title>
  79. <para>
  80. Zend Framework's <acronym>MVC</acronym> layer encourages the use of "modules", which
  81. are self-contained applications within your site. Modules typically have a number of
  82. resource types by default, and Zend Framework even
  83. <link linkend="project-structure.filesystem">recommends a standard directory layout
  84. for modules</link>. Resource autoloaders are therefore
  85. quite useful in this paradigm -- so useful that they are enabled by default when you
  86. create a bootstrap class for your module that extends
  87. <classname>Zend_Application_Module_Bootstrap</classname>. For more information, read
  88. the <link
  89. linkend="zend.loader.autoloader-resource.module">Zend_Loader_Autoloader_Module
  90. documentation</link>.
  91. </para>
  92. </note>
  93. </sect1>