--- layout: default title: URI middlewares redirect_from: - /5.0/manipulations/middlewares/ --- URI middlewares ======= ## Definition An URI middleware is a class which provides a convenient mechanism for filtering and manipulating an URI object. The only **hard** requirement is that a URI middleware **MUST** returns an URI instance of the same type that the one it received. ## Example For instance here's how you would update the query string from a given URI object: ~~~php getQuery(); parse_str($source_query, $params); parse_str($query_to_merge, $new_params); $new_query = http_build_query( array_merge($params, $new_params), '', '&', PHP_QUERY_RFC3986 ); $new_uri = $uri->withQuery($new_query); echo $new_uri; // display http://www.example.com?fo_o=bar&taz=#~typo ~~~ Using the provided `League\Uri\Modifiers\MergeQuery` middleware the code becomes ~~~php process($uri); echo $new_uri; // display http://www.example.com?fo.o=bar&taz=#~typo // $new_uri is a SlimUri object ~~~
~~~php To reduce BC break, all implemented URI middlewares still support the__invoke method. The method is an alias of the process method.
~~~php
withHost('thephpleague.com');
};
$uri = SlimUri::createFromString("http://www.example.com?fo.o=toto#~typo");
$new_uri = (new CallableAdapter($callable))->process($uri);
echo $new_uri; // display http://thephpleague.com?fo.o=toto#~typo
// $new_uri is a SlimUri object
~~~
## Middlewares which manipulate several URI components
### Resolving a relative URI
The `Resolve` URI Middleware provides the mean for resolving an URI as a browser would for a relative URI. When performing URI resolution the returned URI is normalized according to RFC3986 rules. The uri to resolved must be another Uri object.
~~~php
process($relativeUri);
echo $newUri; //displays "http://www.example.com/path/to/the/sky/p#~toto"
~~~
~~~php
process($uri);
echo $relativeUri; // display "/?foo=toto#~typo
echo $resolver->process($relativeUri); // display 'http://www.example.com/?foo=toto#~typo'
~~~
~~~php
League\Uri\Modifiers\Normalize URI Middleware is introduce to normalize URI according to the following rules:
- The host component is converted into their ASCII representation;
- The path component is normalized by removing dot segments as per RFC3986;
- The query component is sorted according to its key offset;
- The scheme component is lowercased;
- Unreserved characters are decoded;
If you normalized two URI objects it become easier to compare them to determine if they are representing the same resource:
~~~php
process($uri);
$newAltUri = $modifier->process($altUri);
var_dump($newUri->__toString() === $newAltUri->__toString()); //return true
~~~
~~~php
You should avoid using the Normalize URI middleware for anything else but URI comparison as some changes applied may introduce some data loss.
### Applying multiple URI middleware to a single URI
Since all URI middleware returns a URI object instance it is possible to chain them together. To ease this chaining the package comes bundle with the `League\Uri\Modifiers\Pipeline` class. The class uses the pipeline pattern to modify the URI by passing the results from one modifier to the next one.
The `League\Uri\Modifiers\Pipeline` uses the `Pipeline::pipe` to attach a URI Middleware following the *First In First Out* rule.
~~~php
pipe(new RemoveDotSegment())
->pipe(new HostToAscii())
->pipe(new KsortQuery());
$origUri1Alt = $modifier->process($origUri1);
$origUri2Alt = $modifier->process($origUri2);
echo $origUri1Alt; //display http://xn--oy2b35ckwhba574atvuzkc.com/to/the/sky/
echo $origUri2Alt; //display http://xn--oy2b35ckwhba574atvuzkc.com/to/the/sky/
~~~
## Query specific URI Middlewares
In addition to modifiying the URI query component, the middleware normalizes the query string to RFC3986
### Sorting the query keys
Sorts the query according to its key values.
#### Using PHP sorting constant
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?foo=bar%20baz&kingkong=toto#doc3"
~~~
#### Using a user defined comparison function like the [uksort function](http://php.net/uksort)
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?foo=bar%20baz&kingkong=toto#doc3"
~~~
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?kingkong=godzilla&foo=bar%20baz&toto#doc3"
~~~
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?kingkong=toto&kingkong=godzilla&foo=bar%20baz&toto#doc3"
~~~
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?kingkong=toto#doc3"
~~~
~~~php
Since version 1.3.0
Removes query params from the current URI query string by providing the param name. The removal preserves mangled key params.
~~~php
process($uri);
echo $newUri; //display "http://example.com/test.php?fo_o=bar"
~~~
The `Uri\remove_params` functions also exists.
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/to/the/sky/"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/to/the/sky/"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path?foo=bar"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/sky/#top"
~~~
~~~php
Uri\add_trailing_slash($uri);
echo $newUri; //display "http://www.example.com/sky/#top"
~~~
### Removing leading slash
Removes the path leading slash if present.
~~~php
process($uri);
echo $newUri; //display "path/to/the/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "/path/to/the/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/road/to/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/to/the/paradise.xml"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/export.csv"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/the/real/path/to/the/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/to/the/sky/and/above"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/and/above/path/to/the/sky/and/above"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/to/the/sea/"
~~~
The previous example can be rewritten using negative offset:
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/to/the/sea"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/the/"
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com/path/the"
~~~
~~~php
process($uri);
echo $newUri; //display "data:text/plain;charset=utf-8,Hello%20World!"
~~~
~~~php
process($uri);
echo $newUri; //display "data:text/plain;charset=US-ASCII;base64,SGVsbG8gV29ybGQh"
~~~
~~~php
process($uri);
echo $newUri; //display "data:text/plain;charset=US-ASCII,Hello%20World!"
~~~
~~~php
process($uri);
echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri
echo $newUri; //display "http://xn--oy2b35ckwhba574atvuzkc.com/to/the/sky/"
~~~
~~~php
process($uri);
echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri
echo $newUri; //display "http://스타벅스코리아.com/to/the/sky/"
~~~
~~~php
process($uri);
echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri
echo $newUri; //display "http://www.bbc.co.uk/foo/bar"
~~~
~~~php
process($uri);
echo $newUri; //display "http://shop.example.com/foo/bar"
~~~
~~~php
process($uri);
echo get_class($newUri); //display \Zend\Diactoros\Uri
echo $newUri; //display 'http://[fe80::1234]/path/to/the/sky.php'
~~~
~~~php
process($uri);
echo $newUri; //display 'http://example.com.:83'
~~~
~~~php
process($uri);
echo $newUri; //display 'http://example.com#yes'
~~~
~~~php
process($uri);
echo $newUri; //display "http://www.example.com.fr/path/to/the/sky/"
~~~
~~~php
process($uri);
echo $newUri; //display "http://shop.www.example.com/path/to/the/sky/and/above"
~~~
~~~php
process($uri);
echo $newUri; //display "http://admin.shop.example.com/path/to/the/sky"
~~~
The previous example can be rewritten using negative offset:
~~~php
process($uri);
echo $newUri; //display "http://admin.shop.example.com/path/to/the/sky"
~~~
~~~php
process($uri);
echo $newUri; //display "http://example.com/path/the/sky/"
~~~
The previous example can be rewritten using negative offset:
~~~php
process($uri);
echo $newUri; //display "http://example.com/path/the/sky/"
~~~
~~~php