Zend_Uri.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.uri.chapter">
  4. <title>Zend_Uri</title>
  5. <sect2 id="zend.uri.overview">
  6. <title>Overview</title>
  7. <para>
  8. <classname>Zend_Uri</classname> is a component that aids in manipulating and validating
  9. <ulink url="http://www.w3.org/Addressing/">Uniform Resource Identifiers</ulink> (URIs).
  10. <classname>Zend_Uri</classname> exists primarily to service other components, such as
  11. <classname>Zend_Http_Client</classname>, but is also useful as a standalone utility.
  12. </para>
  13. <para>
  14. <acronym>URI</acronym>s always begin with a scheme, followed by a colon. The
  15. construction of the many different schemes varies significantly. The
  16. <classname>Zend_Uri</classname> class provides a factory that returns a subclass
  17. of itself which specializes in each scheme. The subclass will be named
  18. <classname>Zend_Uri_&lt;scheme&gt;</classname>, where
  19. <emphasis>&lt;scheme&gt;</emphasis> is the scheme, lowercased with the first letter
  20. capitalized. An exception to this rule is <acronym>HTTPS</acronym>, which is also
  21. handled by <classname>Zend_Uri_Http</classname>.
  22. </para>
  23. </sect2>
  24. <sect2 id="zend.uri.creation">
  25. <title>Creating a New URI</title>
  26. <para>
  27. <classname>Zend_Uri</classname> will build a new <acronym>URI</acronym> from scratch
  28. if only a scheme is passed to <methodname>Zend_Uri::factory()</methodname>.
  29. </para>
  30. <example id="zend.uri.creation.example-1">
  31. <title>Creating a New URI with Zend_Uri::factory()</title>
  32. <programlisting language="php"><![CDATA[
  33. // To create a new URI from scratch, pass only the scheme.
  34. $uri = Zend_Uri::factory('http');
  35. // $uri instanceof Zend_Uri_Http
  36. ]]></programlisting>
  37. </example>
  38. <para>
  39. To create a new <acronym>URI</acronym> from scratch, pass only the scheme to
  40. <methodname>Zend_Uri::factory()</methodname><footnote><para>At the time of writing,
  41. <classname>Zend_Uri</classname> only provides built-in support for the <acronym>HTTP</acronym>
  42. and <acronym>HTTPS</acronym> schemes.</para></footnote>. If an unsupported scheme is
  43. passed and no scheme-specific class is specified, a <classname>Zend_Uri_Exception</classname>
  44. will be thrown.
  45. </para>
  46. <para>
  47. If the scheme or <acronym>URI</acronym> passed is supported,
  48. <methodname>Zend_Uri::factory()</methodname> will return a subclass of itself that
  49. specializes in the scheme to be created.
  50. </para>
  51. <sect3>
  52. <title>Creating a New Custom-Class URI</title>
  53. <para>
  54. Starting from Zend Framework 1.10.5, you can specify a custom class to be
  55. used when creating the Zend_Uri instance, as a second parameter to the
  56. <methodname>Zend_Uri::factory()</methodname> method.
  57. This enables you to subclass Zend_Uri and create your own custom URI classes,
  58. and instantiate new URI objects based on your own custom classes.
  59. </para>
  60. <para>
  61. The 2nd parameter passed to <methodname>Zend_Uri::factory()</methodname> must
  62. be a string with the name of a class extending <classname>Zend_Uri</classname>.
  63. The class must either be alredy-loaded, or loadable using <methodname>Zend_Loader::loadClass()</methodname> -
  64. that is, it must follow the Zend Framework class and file naming conventions, and
  65. must be in your include_path.
  66. </para>
  67. <example id="zend.uri.creation.custom.example-1">
  68. <title>Creating a URI using a custom class</title>
  69. <programlisting language="php"><![CDATA[
  70. // Create a new 'ftp' URI based on a custom class
  71. $ftpUri = Zend_Uri::factory(
  72. 'ftp://user@ftp.example.com/path/file',
  73. 'MyLibrary_Uri_Ftp'
  74. );
  75. // $ftpUri is an instance of MyLibrary_Uri_Ftp, which is a subclass of Zend_Uri
  76. ]]></programlisting>
  77. </example>
  78. </sect3>
  79. </sect2>
  80. <sect2 id="zend.uri.manipulation">
  81. <title>Manipulating an Existing URI</title>
  82. <para>
  83. To manipulate an existing <acronym>URI</acronym>, pass the entire <acronym>URI</acronym>
  84. to <methodname>Zend_Uri::factory()</methodname>.
  85. </para>
  86. <example id="zend.uri.manipulation.example-1">
  87. <title>Manipulating an Existing URI with Zend_Uri::factory()</title>
  88. <programlisting language="php"><![CDATA[
  89. // To manipulate an existing URI, pass it in.
  90. $uri = Zend_Uri::factory('http://www.zend.com');
  91. // $uri instanceof Zend_Uri_Http
  92. ]]></programlisting>
  93. </example>
  94. <para>
  95. The <acronym>URI</acronym> will be parsed and validated. If it is found to be invalid,
  96. a <classname>Zend_Uri_Exception</classname> will be thrown immediately. Otherwise,
  97. <methodname>Zend_Uri::factory()</methodname> will return a subclass of itself that
  98. specializes in the scheme to be manipulated.
  99. </para>
  100. </sect2>
  101. <sect2 id="zend.uri.validation">
  102. <title>URI Validation</title>
  103. <para>
  104. The <methodname>Zend_Uri::check()</methodname> method can only be used if validation
  105. of an existing <acronym>URI</acronym> is needed.
  106. </para>
  107. <example id="zend.uri.validation.example-1">
  108. <title>URI Validation with Zend_Uri::check()</title>
  109. <programlisting language="php"><![CDATA[
  110. // Validate whether a given URI is well formed
  111. $valid = Zend_Uri::check('http://uri.in.question');
  112. // $valid is TRUE for a valid URI, or FALSE otherwise.
  113. ]]></programlisting>
  114. </example>
  115. <para>
  116. <methodname>Zend_Uri::check()</methodname> returns a boolean, which is more convenient
  117. than using <methodname>Zend_Uri::factory()</methodname> and catching the exception.
  118. </para>
  119. <sect3 id="zend.uri.validation.allowunwise">
  120. <title>Allowing "Unwise" characters in URIs</title>
  121. <para>
  122. By default, <classname>Zend_Uri</classname> will not accept the following
  123. characters: <emphasis>"{", "}", "|", "\", "^", "`"</emphasis>. These characters are
  124. defined by the <acronym>RFC</acronym> as "unwise" and invalid; however, many
  125. implementations do accept these characters as valid.
  126. </para>
  127. <para>
  128. <classname>Zend_Uri</classname> can be set to accept these "unwise" characters by
  129. setting the 'allow_unwise' option to boolean <constant>TRUE</constant> using
  130. <methodname>Zend_Uri::setConfig()</methodname>:
  131. </para>
  132. <example id="zend.uri.validation.allowunwise.example-1">
  133. <title>Allowing special characters in URIs</title>
  134. <programlisting language="php"><![CDATA[
  135. // Contains '|' symbol
  136. // Normally, this would return false:
  137. $valid = Zend_Uri::check('http://example.com/?q=this|that');
  138. // However, you can allow "unwise" characters
  139. Zend_Uri::setConfig(array('allow_unwise' => true));
  140. // will return 'true'
  141. $valid = Zend_Uri::check('http://example.com/?q=this|that');
  142. // Reset the 'allow_unwise' value to the default FALSE
  143. Zend_Uri::setConfig(array('allow_unwise' => false));
  144. ]]></programlisting>
  145. </example>
  146. <note>
  147. <para>
  148. <methodname>Zend_Uri::setConfig()</methodname> sets configuration options
  149. globally. It is recommended to reset the 'allow_unwise' option to
  150. '<constant>FALSE</constant>', like in the example above, unless you are certain
  151. you want to always allow unwise characters globally.
  152. </para>
  153. </note>
  154. </sect3>
  155. </sect2>
  156. <sect2 id="zend.uri.instance-methods">
  157. <title>Common Instance Methods</title>
  158. <para>
  159. Every instance of a <classname>Zend_Uri</classname> subclass (e.g.
  160. <classname>Zend_Uri_Http</classname>) has several instance methods that are useful for
  161. working with any kind of <acronym>URI</acronym>.
  162. </para>
  163. <sect3 id="zend.uri.instance-methods.getscheme">
  164. <title>Getting the Scheme of the URI</title>
  165. <para>
  166. The scheme of the <acronym>URI</acronym> is the part of the <acronym>URI</acronym>
  167. that precedes the colon. For example, the scheme of
  168. <filename>http://www.zend.com</filename> is 'http'.
  169. </para>
  170. <example id="zend.uri.instance-methods.getscheme.example-1">
  171. <title>Getting the Scheme from a Zend_Uri_* Object</title>
  172. <programlisting language="php"><![CDATA[
  173. $uri = Zend_Uri::factory('http://www.zend.com');
  174. $scheme = $uri->getScheme(); // "http"
  175. ]]></programlisting>
  176. </example>
  177. <para>
  178. The <methodname>getScheme()</methodname> instance method returns only the scheme
  179. part of the <acronym>URI</acronym> object.
  180. </para>
  181. </sect3>
  182. <sect3 id="zend.uri.instance-methods.geturi">
  183. <title>Getting the Entire URI</title>
  184. <example id="zend.uri.instance-methods.geturi.example-1">
  185. <title>Getting the Entire URI from a Zend_Uri_* Object</title>
  186. <programlisting language="php"><![CDATA[
  187. $uri = Zend_Uri::factory('http://www.zend.com');
  188. echo $uri->getUri(); // "http://www.zend.com"
  189. ]]></programlisting>
  190. </example>
  191. <para>
  192. The <methodname>getUri()</methodname> method returns the string representation
  193. of the entire <acronym>URI</acronym>.
  194. </para>
  195. </sect3>
  196. <sect3 id="zend.uri.instance-methods.valid">
  197. <title>Validating the URI</title>
  198. <para>
  199. <methodname>Zend_Uri::factory()</methodname> will always validate any
  200. <acronym>URI</acronym> passed to it and will not instantiate a new
  201. <classname>Zend_Uri</classname> subclass if the given <acronym>URI</acronym> is
  202. found to be invalid. However, after the <classname>Zend_Uri</classname> subclass is
  203. instantiated for a new <acronym>URI</acronym> or an existing valid one, it is
  204. possible that the <acronym>URI</acronym> can later become invalid after it
  205. is manipulated.
  206. </para>
  207. <example id="zend.uri.instance-methods.valid.example-1">
  208. <title>Validating a Zend_Uri_* Object</title>
  209. <programlisting language="php"><![CDATA[
  210. $uri = Zend_Uri::factory('http://www.zend.com');
  211. $isValid = $uri->valid(); // TRUE
  212. ]]></programlisting>
  213. </example>
  214. <para>
  215. The <methodname>valid()</methodname> instance method provides a means to check that
  216. the <acronym>URI</acronym> object is still valid.
  217. </para>
  218. </sect3>
  219. </sect2>
  220. </sect1>
  221. <!--
  222. vim:se ts=4 sw=4 et:
  223. -->