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