--- 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 ~~~

Since version 1.1.0 The above code can ben even more simplyfied

~~~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" ~~~

Since version 1.1.0 The alias function Uri\resolve is available

~~~php process($uri); echo $relativeUri; // display "/?foo=toto#~typo echo $resolver->process($relativeUri); // display 'http://www.example.com/?foo=toto#~typo' ~~~

Since version 1.1.0 The alias function Uri\relativize is available

~~~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 ~~~

Since version 1.1.0 The alias function Uri\normalize is available

~~~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/ ~~~

The League\Uri\Modifiers\Pipeline is a URI middleware as well which can lead to advance modifications from you URI in a sane an normalized way.

This class is heavily influenced by the League\Pipeline package.

## 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" ~~~

Since version 1.1.0 The alias function Uri\sort_query is available

~~~php process($uri); echo $newUri; //display "http://example.com/test.php?kingkong=godzilla&foo=bar%20baz&toto#doc3" ~~~

Since version 1.1.0 The alias function Uri\merge_query is available

~~~php process($uri); echo $newUri; //display "http://example.com/test.php?kingkong=toto&kingkong=godzilla&foo=bar%20baz&toto#doc3" ~~~

Since version 1.1.0 The alias function Uri\append_query is available

~~~php process($uri); echo $newUri; //display "http://example.com/test.php?kingkong=toto#doc3" ~~~

Since version 1.1.0 The alias function Uri\remove_pairs is available

~~~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/" ~~~

Since version 1.1.0 The alias function Uri\remove_dot_segments is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/to/the/sky/" ~~~

Since version 1.1.0 The alias function Uri\remove_empty_segments is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path?foo=bar" ~~~

Since version 1.1.0 The alias function Uri\remove_trailing_slash is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/sky/#top" ~~~

Since version 1.1.0 The alias function Uri\add_trailing_slash is available

~~~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" ~~~

Since version 1.1.0 The alias function Uri\remove_leading_slash is available

~~~php process($uri); echo $newUri; //display "/path/to/the/sky" ~~~

Since version 1.1.0 The alias function Uri\add_leading_slash is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/road/to/sky" ~~~

Since version 1.1.0 The alias function Uri\replace_dirname is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/to/the/paradise.xml" ~~~

Since version 1.1.0 The alias function Uri\replace_basename is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/export.csv" ~~~

Since version 1.1.0 The alias function Uri\replace_extension is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/the/real/path/to/the/sky" ~~~

Since version 1.1.0 The alias function Uri\add_basepath is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/sky" ~~~

Since version 1.1.0 The alias function Uri\remove_basepath is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/to/the/sky/and/above" ~~~

Since version 1.1.0 The alias function Uri\append_path is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/and/above/path/to/the/sky/and/above" ~~~

Since version 1.1.0 The alias function Uri\prepend_path is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/to/the/sea/" ~~~

This URI middleware supports negative offset

The previous example can be rewritten using negative offset: ~~~php process($uri); echo $newUri; //display "http://www.example.com/path/to/the/sea" ~~~

Since version 1.1.0 The alias function Uri\replace_segment is available

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/the/" ~~~

This URI middleware supports negative offset

~~~php process($uri); echo $newUri; //display "http://www.example.com/path/the" ~~~

Since version 1.1.0 The alias function Uri\remove_segments is available

~~~php process($uri); echo $newUri; //display "data:text/plain;charset=utf-8,Hello%20World!" ~~~

Since version 1.1.0 The alias function Uri\replace_data_uri_parameters is available

~~~php process($uri); echo $newUri; //display "data:text/plain;charset=US-ASCII;base64,SGVsbG8gV29ybGQh" ~~~

Since version 1.1.0 The alias function Uri\path_to_binary is available

~~~php process($uri); echo $newUri; //display "data:text/plain;charset=US-ASCII,Hello%20World!" ~~~

Since version 1.1.0 The alias function Uri\path_to_ascii is available

~~~php process($uri); echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri echo $newUri; //display "http://xn--oy2b35ckwhba574atvuzkc.com/to/the/sky/" ~~~

This middleware will have no effect on League URI objects as this conversion is done by default.

Since version 1.1.0 The alias function Uri\host_to_ascii is available

~~~php process($uri); echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri echo $newUri; //display "http://스타벅스코리아.com/to/the/sky/" ~~~

This middleware will have no effect on League URI objects because the object always transcode the host component into its RFC3986/ascii representation.

Since version 1.1.0 The alias function Uri\host_to_unicode is available

~~~php process($uri); echo get_class($newUri); //display \GuzzleHttp\Psr7\Uri echo $newUri; //display "http://www.bbc.co.uk/foo/bar" ~~~

Since version 1.1.0 The alias function Uri\replace_registrabledomain is available

~~~php process($uri); echo $newUri; //display "http://shop.example.com/foo/bar" ~~~

Since version 1.1.0 The alias function Uri\replace_subdomain is available

~~~php process($uri); echo get_class($newUri); //display \Zend\Diactoros\Uri echo $newUri; //display 'http://[fe80::1234]/path/to/the/sky.php' ~~~

Since version 1.1.0 The alias function Uri\remove_zone_id is available

~~~php process($uri); echo $newUri; //display 'http://example.com.:83' ~~~

Since version 1.1.0 The alias function Uri\add_root_label is available

~~~php process($uri); echo $newUri; //display 'http://example.com#yes' ~~~

Since version 1.1.0 The alias function Uri\remove_root_label is available

~~~php process($uri); echo $newUri; //display "http://www.example.com.fr/path/to/the/sky/" ~~~

Since version 1.1.0 The alias function Uri\append_host is available

~~~php process($uri); echo $newUri; //display "http://shop.www.example.com/path/to/the/sky/and/above" ~~~

Since version 1.1.0 The alias function Uri\prepend_host is available

~~~php process($uri); echo $newUri; //display "http://admin.shop.example.com/path/to/the/sky" ~~~

The host is considered as a hierarchical component, labels are indexed from right to left according to host RFC

This URI middleware supports negative offset

The previous example can be rewritten using negative offset: ~~~php process($uri); echo $newUri; //display "http://admin.shop.example.com/path/to/the/sky" ~~~

Since version 1.1.0 The alias function Uri\replace_label is available

~~~php process($uri); echo $newUri; //display "http://example.com/path/the/sky/" ~~~

The host is considered as a hierarchical component, labels are indexed from right to left according to host RFC

This URI middleware supports negative offset

The previous example can be rewritten using negative offset: ~~~php process($uri); echo $newUri; //display "http://example.com/path/the/sky/" ~~~

Since version 1.1.0 The alias function Uri\remove_labels is available

~~~php