1- # Zend\\ Uri
2-
3- ## Overview
4-
5- ` Zend\Uri ` is a component that aids in manipulating and validating [ Uniform
6- Resource Identifiers] ( http://www.w3.org/Addressing/ )
7- ([ URIs] ( http://www.ietf.org/rfc/rfc3986.txt ) ). ` Zend\Uri ` exists primarily
8- to service other components, such as ` Zend\Http ` , but is also useful as a
9- standalone utility.
10-
11- * URI* s always begin with a scheme, followed by a colon. The construction of the
12- many different schemes varies significantly. The ` Zend\Uri ` component provides
13- the ` Zend\Uri\UriFactory ` that returns a class implementing the
14- ` Zend\Uri\UriInterface ` which specializes in the scheme if such a class is
15- registered with the Factory.
1+ # Usage
162
173## Creating a New URI
184
@@ -29,38 +15,41 @@ $uri = Zend\Uri\UriFactory::factory('http:');
2915// $uri instanceof Zend\Uri\UriInterface
3016```
3117
32- To create a new * URI* from scratch, pass only the scheme followed by a colon to
18+ To create a new URI from scratch, pass only the scheme followed by a colon to
3319` Zend\Uri\UriFactory::factory() ` . If an unsupported scheme is passed and no
3420scheme-specific class is specified, a
3521` Zend\Uri\Exception\InvalidArgumentException ` will be thrown.
3622
37- If the scheme or * URI* passed is supported, ` Zend\Uri\UriFactory::factory() `
38- will return a class implementing ` Zend\Uri\UriInterface ` that specializes in the
39- scheme to be created .
23+ If the scheme or URI passed is supported, ` Zend\Uri\UriFactory::factory() ` will
24+ return a class implementing ` Zend\Uri\UriInterface ` that specializes in the
25+ scheme referenced .
4026
41- > ### Note
42- At the time of writing, ` Zend\Uri ` provides built-in support for the following
43- schemes: HTTP, HTTPS, MAILTO and FILE
27+ > ### Supported schemes
28+ >
29+ > At the time of writing, zend-uri provides built-in support for the following
30+ > schemes only: HTTP, HTTPS, MAILTO and FILE.
4431
4532### Creating a New Custom-Class URI
4633
4734You can specify a custom class to be used when using the ` Zend\Uri\UriFactory `
48- by registering your class with the Factory using
49- ` Zend\Uri\UriFactory::registerScheme() ` which takes the scheme as first
50- parameter. This enables you to create your own * URI* - class and instantiate new
51- * URI * objects based on your own custom classes.
35+ by registering your class with the ` UriFactory ` using
36+ ` Zend\Uri\UriFactory::registerScheme($scheme, $class) ` . This enables you to
37+ create your own URI class and instantiate new URI objects based on your own
38+ custom classes.
5239
5340The 2nd parameter passed to ` Zend\Uri\UriFactory::registerScheme() ` must be a
5441string with the name of a class implementing ` Zend\Uri\UriInterface ` . The class
5542must either be already loaded, or be loadable by the autoloader.
5643
5744#### Creating a URI using a custom class
5845
46+ The following registers the ` ftp ` scheme with a custom URI class:
47+
5948``` php
60- // Create a new 'ftp' URI based on a custom class
49+ use MyNamespace\MyClass;
6150use Zend\Uri\UriFactory
6251
63- UriFactory::registerScheme('ftp', 'MyNamespace\ MyClass' );
52+ UriFactory::registerScheme('ftp', MyClass::class );
6453
6554$ftpUri = UriFactory::factory(
6655 'ftp://
[email protected] /path/file'
@@ -72,135 +61,126 @@ $ftpUri = UriFactory::factory(
7261
7362## Manipulating an Existing URI
7463
75- To manipulate an existing * URI* , pass the entire * URI* as string to
76- ` Zend\Uri\UriFactory::factory() ` .
64+ To manipulate an existing URI, pass the entire URI as a string to
65+ ` Zend\Uri\UriFactory::factory() ` , and then manipulate the instance returned .
7766
7867### Manipulating an Existing URI with Zend\\ Uri\\ UriFactory::factory()
7968
8069``` php
70+ use Zend\Uri\UriFactory;
71+
8172// To manipulate an existing URI, pass it in.
82- $uri = Zend\Uri\ UriFactory::factory('http://www.zend.com');
73+ $uri = UriFactory::factory('http://www.zend.com');
8374
8475// $uri instanceof Zend\Uri\UriInterface
8576```
8677
87- The * URI* will be parsed and validated. If it is found to be invalid, a
78+ The URI will be parsed and validated. If it is found to be invalid, a
8879` Zend\Uri\Exception\InvalidArgumentException ` will be thrown immediately.
8980Otherwise, ` Zend\Uri\UriFactory::factory() ` will return a class implementing
9081` Zend\Uri\UriInterface ` that specializes in the scheme to be manipulated.
9182
9283## Common Instance Methods
9384
9485The ` Zend\Uri\UriInterface ` defines several instance methods that are useful for
95- working with any kind of * URI* .
86+ working with any kind of URI.
9687
9788### Getting the Scheme of the URI
9889
99- The scheme of the * URI* is the part of the * URI* that precedes the colon. For
90+ The scheme of the URI is the part of the URI that precedes the colon. For
10091example, the scheme of
` http://[email protected] /my/path?query#token ` is
10192'http'.
10293
103- #### Getting the Scheme from a Zend\\ Uri\\ UriInterface Object
104-
10594``` php
10695$uri = Zend\Uri\UriFactory::factory('mailto:
[email protected] ');
10796
10897$scheme = $uri->getScheme(); // "mailto"
10998```
11099
111- The ` getScheme() ` instance method returns only the scheme part of the * URI*
112- object.
100+ The ` getScheme() ` instance method returns only the scheme part of the URI
101+ object (not the separator) .
113102
114103### Getting the Userinfo of the URI
115104
116- The userinfo of the * URI* is the optional part of the * URI* that follows the
105+ The userinfo of the URI is the optional part of the URI that follows the
117106colon and comes before the host-part. For example, the userinfo of
118107` http://[email protected] /my/path?query#token ` is 'johndoe'.
119108
120- #### Getting the Username from a Zend\Uri\UriInterface Object
121-
122109``` php
123110$uri = Zend\Uri\UriFactory::factory('mailto:
[email protected] ');
124111
125112$scheme = $uri->getUserinfo(); // "john.doe"
126113```
127114
128- The ` getUserinfo() ` method returns only the userinfo part of the * URI* object.
115+ The ` getUserinfo() ` method returns only the userinfo part of the URI object.
129116
130117### Getting the host of the URI
131118
132- The host of the * URI* is the optional part of the * URI* that follows the
119+ The host of the URI is the optional part of the URI that follows the
133120user-part and comes before the path-part. For example, the host of
134121` http://[email protected] /my/path?query#token ` is 'example.com'.
135122
136- #### Getting the host from a Zend\\ Uri\\ UriInterface Object
137-
138123``` php
139124$uri = Zend\Uri\UriFactory::factory('mailto:
[email protected] ');
140125
141126$scheme = $uri->getHost(); // "example.com"
142127```
143128
144- The ` getHost() ` method returns only the host part of the * URI* object.
129+ The ` getHost() ` method returns only the host part of the URI object.
145130
146131### Getting the port of the URI
147132
148- The port of the * URI* is the optional part of the * URI* that follows the
149- host-part and comes before the path-part. For example, the host of
150- ` http://[email protected] :80/my/path?query#token ` is '80'. The
* URI
* -class can
151- define default-ports that can be returned when no port is given in the * URI* .
152-
153- #### Getting the port from a Zend\\ Uri\\ UriInterface Object
133+ The port of the URI is the optional part of the URI that follows the host-part
134+ and comes before the path-part. For example, the host of
135+ ` http://[email protected] :80/my/path?query#token ` is '80'.
154136
155137``` php
156138$uri = Zend\Uri\UriFactory::factory('http://example.com:8080');
157139
158140$scheme = $uri->getPort(); // "8080"
159141```
160142
161- #### Getting a default port from a Zend\\ Uri\\ UriInterface Object
143+ Concrete URI instances can define default ports that can be returned when no
144+ port is given in the URI:
162145
163146``` php
164147$uri = Zend\Uri\UriFactory::factory('http://example.com');
165148
166149$scheme = $uri->getPort(); // "80"
167150```
168151
169- The ` getHost() ` method returns only the port part of the * URI* object.
152+ The ` getHost() ` method returns only the port part of the URI object.
170153
171154### Getting the path of the URI
172155
173- The path of the * URI* is the mandatory part of the * URI* that follows the port
156+ The path of the URI is a mandatory part of the URI that follows the port
174157and comes before the query-part. For example, the path of
175158` http://[email protected] :80/my/path?query#token ` is '/my/path'.
176159
177- #### Getting the path from a Zend\\ Uri\\ UriInterface Object
178-
179160``` php
180161$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
181162
182163$scheme = $uri->getPath(); // "/my/path"
183164```
184165
185- The ` getPath() ` method returns only the path of the * URI* object.
166+ The ` getPath() ` method returns only the path of the URI object.
186167
187168### Getting the query-part of the URI
188169
189- The query-part of the * URI* is the optional part of the * URI* that follows the
170+ The query-part of the URI is an optional part of the URI that follows the
190171path and comes before the fragment. For example, the query of
191- ` http://[email protected] :80/my/path?query#token ` is 'quer'.
192-
193- #### Getting the query from a Zend\\ Uri\\ UriInterface Object
172+ ` http://[email protected] :80/my/path?query#token ` is 'query'.
194173
195174``` php
196175$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
197176
198177$scheme = $uri->getQuery(); // "a=b&c=d"
199178```
200179
201- The ` getQuery() ` method returns only the query-part of the * URI* object.
180+ The ` getQuery() ` method returns only the query-part of the URI object.
202181
203- #### Getting the query as array from a Zend\\ Uri\\ UriInterface Object
182+ The query string often contains key=value pairs and therefore can be split into an
183+ associative array. This array can be retrieved using ` getQueryAsArray() ` :
204184
205185``` php
206186$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
@@ -212,28 +192,23 @@ $scheme = $uri->getQueryAsArray();
212192// ]
213193```
214194
215- The query-part often contains key=value pairs and therefore can be split into an
216- associative array. This array can be retrieved using ` getQueryAsArray() ` .
217-
218195### Getting the fragment-part of the URI
219196
220- The fragment-part of the * URI* is the optional part of the * URI* that follows
197+ The fragment-part of the URI is an optional part of the URI that follows
221198the query. For example, the fragment of
222199` http://[email protected] :80/my/path?query#token ` is 'token'.
223200
224- #### Getting the fragment from a Zend\\ Uri\\ UriInterface Object
225-
226201``` php
227202$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
228203
229204$scheme = $uri->getFragment(); // "token"
230205```
231206
232- The ` getFragment() ` method returns only the fragment-part of the * URI* object.
207+ The ` getFragment() ` method returns only the fragment-part of the URI object.
233208
234209### Getting the Entire URI
235210
236- #### Getting the Entire URI from a Zend \\ Uri \\ UriInterface Object
211+ The ` toString() ` method returns the string representation of the entire * URI * .
237212
238213``` php
239214$uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
@@ -244,27 +219,23 @@ echo $uri->toString(); // "http://www.zend.com"
244219echo (string) $uri; // "http://www.zend.com"
245220```
246221
247- The ` toString() ` method returns the string representation of the entire * URI* .
248-
249- The ` Zend\Uri\UriInterface ` defines also a magic ` __toString() ` method that
250- returns the string representation of the * URI* when the Object is cast to a
222+ The ` Zend\Uri\UriInterface ` defines also the magic ` __toString() ` method that
223+ returns the string representation of the URI when the object is cast to a
251224string.
252225
253- ### Validating the URI
226+ ## Validating the URI
254227
255- When using ` Zend\Uri\UriFactory::factory() ` the given * URI* will always be
228+ When using ` Zend\Uri\UriFactory::factory() ` , the given URI will always be
256229validated and a ` Zend\Uri\Exception\InvalidArgumentException ` will be thrown
257- when the * URI* is invalid. However, after the ` Zend\Uri\UriInterface ` is
258- instantiated for a new * URI* or an existing valid one, it is possible that the
259- * URI* can later become invalid after it is manipulated.
260-
261- #### Validating a ZendUri* Object
230+ when the URI is invalid. However, after the ` Zend\Uri\UriInterface ` is
231+ instantiated for a new URI or an existing valid one, it is possible that the URI
232+ can later become invalid after it is manipulated.
262233
263234``` php
264235$uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
265236
266237$isValid = $uri->isValid(); // TRUE
267238```
268239
269- The ` isValid() ` instance method provides a means to check that the * URI* object
270- is still valid.
240+ The ` isValid() ` instance method provides a means to check that the URI object
241+ is still valid.
0 commit comments