Zend_Uri.xml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <code>&lt;scheme&gt;</code>
  19. is the scheme, lowercased with the first letter capitalized. An exception to this
  20. rule is <acronym>HTTPS</acronym>, which is also handled by
  21. <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 supports the <acronym>HTTP</acronym> and
  42. <acronym>HTTPS</acronym> schemes.</para></footnote>. If an unsupported scheme is
  43. passed, a <classname>Zend_Uri_Exception</classname> will be thrown.
  44. </para>
  45. <para>
  46. If the scheme or <acronym>URI</acronym> passed is supported,
  47. <methodname>Zend_Uri::factory()</methodname> will return a subclass of itself that
  48. specializes in the scheme to be created.
  49. </para>
  50. </sect2>
  51. <sect2 id="zend.uri.manipulation">
  52. <title>Manipulating an Existing URI</title>
  53. <para>
  54. To manipulate an existing <acronym>URI</acronym>, pass the entire <acronym>URI</acronym>
  55. to <methodname>Zend_Uri::factory()</methodname>.
  56. </para>
  57. <example id="zend.uri.manipulation.example-1">
  58. <title>Manipulating an Existing URI with Zend_Uri::factory()</title>
  59. <programlisting language="php"><![CDATA[
  60. // To manipulate an existing URI, pass it in.
  61. $uri = Zend_Uri::factory('http://www.zend.com');
  62. // $uri instanceof Zend_Uri_Http
  63. ]]></programlisting>
  64. </example>
  65. <para>
  66. The <acronym>URI</acronym> will be parsed and validated. If it is found to be invalid,
  67. a <classname>Zend_Uri_Exception</classname> will be thrown immediately. Otherwise,
  68. <methodname>Zend_Uri::factory()</methodname> will return a subclass of itself that
  69. specializes in the scheme to be manipulated.
  70. </para>
  71. </sect2>
  72. <sect2 id="zend.uri.validation">
  73. <title>URI Validation</title>
  74. <para>
  75. The <methodname>Zend_Uri::check()</methodname> method can only be used if validation
  76. of an existing <acronym>URI</acronym> is needed.
  77. </para>
  78. <example id="zend.uri.validation.example-1">
  79. <title>URI Validation with Zend_Uri::check()</title>
  80. <programlisting language="php"><![CDATA[
  81. // Validate whether a given URI is well formed
  82. $valid = Zend_Uri::check('http://uri.in.question');
  83. // $valid is TRUE for a valid URI, or FALSE otherwise.
  84. ]]></programlisting>
  85. </example>
  86. <para>
  87. <methodname>Zend_Uri::check()</methodname> returns a boolean, which is more convenient
  88. than using <methodname>Zend_Uri::factory()</methodname> and catching the exception.
  89. </para>
  90. <sect3 id="zend.uri.validation.allowunwise">
  91. <title>Allowing "Unwise" characters in URIs</title>
  92. <para>
  93. By default, <classname>Zend_Uri</classname> will not accept the following
  94. characters: <code>"{", "}", "|", "\", "^", "`"</code>. These characters are defined
  95. by the <acronym>RFC</acronym> as "unwise" and invalid; however, many
  96. implementations do accept these characters as valid.
  97. </para>
  98. <para>
  99. <classname>Zend_Uri</classname> can be set to accept these "unwise" characters by
  100. setting the 'allow_unwise' option to boolean <constant>TRUE</constant> using
  101. <methodname>Zend_Uri::setConfig()</methodname>:
  102. </para>
  103. <example id="zend.uri.validation.allowunwise.example-1">
  104. <title>Allowing special characters in URIs</title>
  105. <programlisting language="php"><![CDATA[
  106. // Contains '|' symbol
  107. // Normally, this would return false:
  108. $valid = Zend_Uri::check('http://example.com/?q=this|that');
  109. // However, you can allow "unwise" characters
  110. Zend_Uri::setConfig(array('allow_unwise' => true));
  111. // will return 'true'
  112. $valid = Zend_Uri::check('http://example.com/?q=this|that');
  113. // Reset the 'allow_unwise' value to the default FALSE
  114. Zend_Uri::setConfig(array('allow_unwise' => false));
  115. ]]></programlisting>
  116. </example>
  117. <note>
  118. <para>
  119. <methodname>Zend_Uri::setConfig()</methodname> sets configuration options
  120. globally. It is recommended to reset the 'allow_unwise' option to
  121. '<constant>FALSE</constant>', like in the example above, unless you are certain
  122. you want to always allow unwise characters globally.
  123. </para>
  124. </note>
  125. </sect3>
  126. </sect2>
  127. <sect2 id="zend.uri.instance-methods">
  128. <title>Common Instance Methods</title>
  129. <para>
  130. Every instance of a <classname>Zend_Uri</classname> subclass (e.g.
  131. <classname>Zend_Uri_Http</classname>) has several instance methods that are useful for
  132. working with any kind of <acronym>URI</acronym>.
  133. </para>
  134. <sect3 id="zend.uri.instance-methods.getscheme">
  135. <title>Getting the Scheme of the URI</title>
  136. <para>
  137. The scheme of the <acronym>URI</acronym> is the part of the <acronym>URI</acronym>
  138. that precedes the colon. For example, the scheme of
  139. <code>http://www.zend.com</code> is <code>http</code>.
  140. </para>
  141. <example id="zend.uri.instance-methods.getscheme.example-1">
  142. <title>Getting the Scheme from a Zend_Uri_* Object</title>
  143. <programlisting language="php"><![CDATA[
  144. $uri = Zend_Uri::factory('http://www.zend.com');
  145. $scheme = $uri->getScheme(); // "http"
  146. ]]></programlisting>
  147. </example>
  148. <para>
  149. The <methodname>getScheme()</methodname> instance method returns only the scheme
  150. part of the <acronym>URI</acronym> object.
  151. </para>
  152. </sect3>
  153. <sect3 id="zend.uri.instance-methods.geturi">
  154. <title>Getting the Entire URI</title>
  155. <example id="zend.uri.instance-methods.geturi.example-1">
  156. <title>Getting the Entire URI from a Zend_Uri_* Object</title>
  157. <programlisting language="php"><![CDATA[
  158. $uri = Zend_Uri::factory('http://www.zend.com');
  159. echo $uri->getUri(); // "http://www.zend.com"
  160. ]]></programlisting>
  161. </example>
  162. <para>
  163. The <methodname>getUri()</methodname> method returns the string representation
  164. of the entire <acronym>URI</acronym>.
  165. </para>
  166. </sect3>
  167. <sect3 id="zend.uri.instance-methods.valid">
  168. <title>Validating the URI</title>
  169. <para>
  170. <methodname>Zend_Uri::factory()</methodname> will always validate any
  171. <acronym>URI</acronym> passed to it and will not instantiate a new
  172. <classname>Zend_Uri</classname> subclass if the given <acronym>URI</acronym> is
  173. found to be invalid. However, after the <classname>Zend_Uri</classname> subclass is
  174. instantiated for a new <acronym>URI</acronym> or an existing valid one, it is
  175. possible that the <acronym>URI</acronym> can later become invalid after it
  176. is manipulated.
  177. </para>
  178. <example id="zend.uri.instance-methods.valid.example-1">
  179. <title>Validating a Zend_Uri_* Object</title>
  180. <programlisting language="php"><![CDATA[
  181. $uri = Zend_Uri::factory('http://www.zend.com');
  182. $isValid = $uri->valid(); // TRUE
  183. ]]></programlisting>
  184. </example>
  185. <para>
  186. The <methodname>valid()</methodname> instance method provides a means to check that
  187. the <acronym>URI</acronym> object is still valid.
  188. </para>
  189. </sect3>
  190. </sect2>
  191. </sect1>
  192. <!--
  193. vim:se ts=4 sw=4 et:
  194. -->