Zend_Uri.xml 8.1 KB

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