The Url component provides basic operations to handle urls (including parse,
build, get/set path parameters, get/set query and create formatted urls).
Do not work with the path, params and query properties directly, because this
will not work in PHP 5.2.0 (that is, do not set/get $url->query[0], because a
notice will be thrown: "Notice: Indirect modification of overloaded property
ezcUrl::$query has no effect"). Instead, use the following methods.
By using the ezcUrlConfiguration class, you can specify a custom configuration
that can be used to parse urls. The properties you can set in objects of this
class are the default base directory, default script name (eg. index.php)
(which will be hidden when building the url by default, but can be displayed
by calling buildUrl( true )), delimiters for unordered parameter names and
names for accepted parameters.
Here is an example of getting the query part of urls:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create a new Url object from a string url
- $url = new ezcUrl( 'http://www.example.com/mydir/index.php/content/view/article/42/mode/print?user[name]=Bob+Smith&user[age]=47&user[sex]=M' );
-
- // get the query parts
- var_dump( $url->getQuery() );
-
- ?>
The output would be:
array(1) {
["user"]=>
array(3) {
["name"]=>
string(9) "Bob Smith"
["age"]=>
string(2) "47"
["sex"]=>
string(1) "M"
}
}
Here is an example of setting the query part of urls:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create a new Url object from a string url
- $url = new ezcUrl( 'http://www.example.com/mydir/index.php/content/view/article/42/mode/print?user[name]=Bob+Smith&user[age]=47&user[sex]=M' );
-
- // create an array which will be used to set the query part
- $query = array( 'user' => array( 'name' => 'Bob Smith',
- 'age' => '47',
- 'sex' => 'M',
- 'dob' => '5/12/1956'),
- );
-
- // set the query part of the Url object
- $url->setQuery( $query );
- var_dump( rawurldecode( $url ) );
-
- // add a query parameter to the query part
- $url->setQuery( array_merge( $url->getQuery(), array( 'sort' => 'desc' ) ) );
- var_dump( rawurldecode( $url ) );
-
- // remove a query parameter from the query part
- $url->setQuery( array_diff_key( $url->getQuery(), array( 'sort' => null ) ) );
- var_dump( rawurldecode( $url ) );
-
- ?>
The output would be as follows (wrapped for clarity):
string(139) "http://www.example.com/mydir/index.php/content/view/article/
42/mode/print?user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=
5/12/1956"
string(149) "http://www.example.com/mydir/index.php/content/view/article/
42/mode/print?user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=
5/12/1956&sort=desc"
string(139) "http://www.example.com/mydir/index.php/content/view/article/
42/mode/print?user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=
5/12/1956"
The following example creates a custom url configuration and uses it when
creating a new url object:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create an ezcUrlConfiguration object
- $urlCfg = new ezcUrlConfiguration();
-
- // set the basedir and script values
- $urlCfg->basedir = 'mydir';
- $urlCfg->script = 'index.php';
-
- // define delimiters for unordered parameter names
- $urlCfg->unorderedDelimiters = array( '(', ')' );
-
- // define ordered parameters
- $urlCfg->addOrderedParameter( 'section' );
- $urlCfg->addOrderedParameter( 'group' );
- $urlCfg->addOrderedParameter( 'category' );
- $urlCfg->addOrderedParameter( 'subcategory' );
-
- // define unordered parameters
- $urlCfg->addUnorderedParameter( 'game' );
-
- // visualize the $urlCfg object
- var_dump( $urlCfg );
-
- // create a new ezcUrl object from a string url and use the above $urlCfg
- $url = new ezcUrl( 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7', $urlCfg );
-
- ?>
The output would be:
object(ezcUrlConfiguration)#1 (1) {
["properties:private"]=>
array(5) {
["basedir"]=>
string(5) "mydir"
["script"]=>
string(9) "index.php"
["unorderedDelimiters"]=>
array(2) {
[0]=>
string(1) "("
[1]=>
string(1) ")"
}
["orderedParameters"]=>
array(4) {
["section"]=>
int(0)
["group"]=>
int(1)
["category"]=>
int(2)
["subcategory"]=>
int(3)
}
["unorderedParameters"]=>
array(1) {
["game"]=>
int(0)
}
}
}
Lazy initialization is a mechanism to load and configure a component, only
when it is really used in your application. This mechanism saves time for
parsing the classes and configuration, when the component is not used at all
during one request. You can find a description how you can use it for your
own components and how it works in the ezcBase tutorial. The keyword for
the url component is ezcUrlConfiguration.
- <?php
- require_once 'tutorial_autoload.php';
-
- class customLazyUrlConfiguration implements ezcBaseConfigurationInitializer
- {
- public static function configureObject( $urlCfg )
- {
- // set the basedir and script values
- $urlCfg->basedir = 'mydir';
- $urlCfg->script = 'index.php';
-
- // define delimiters for unordered parameter names
- $urlCfg->unorderedDelimiters = array( '(', ')' );
-
- // define ordered parameters
- $urlCfg->addOrderedParameter( 'section' );
- $urlCfg->addOrderedParameter( 'group' );
- $urlCfg->addOrderedParameter( 'category' );
- $urlCfg->addOrderedParameter( 'subcategory' );
-
- // define unordered parameters
- $urlCfg->addUnorderedParameter( 'game' );
- }
- }
-
- ezcBaseInit::setCallback(
- 'ezcUrlConfiguration',
- 'customLazyUrlConfiguration'
- );
-
- // Classes loaded and configured on first request
- $url = new ezcUrl(
- 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7',
- ezcUrlConfiguration::getInstance()
- );
- ?>
This examples configures the URL component exactly like the example before.
The main difference is, that we roll out the configuration to an own class,
and define a callback using ezcBaseInit::setCallback to this class, which
will be called with a url configuration instance as the first parameter on
the first call on ezcUrlConfiguration::getInstance().
ezcBaseInit::setCallback accepts as a first parameter a component specific key,
which lets the component later request the right configuration callback. The
second parameter is the name of the class to perform the static callback on.
This class must implement the ezcBaseConfigurationInitializer class.
Each component's lazy initialization calls the static method configureObject()
on the referenced class.
When the URL is really parsed in your application, like shown in line 35 of
the example, a new ezcUrlConfiguration is instatiated an automatically
configured by the configureObject() method.
The following example uses the custom url configuration from before to get
the parameters from the provided url:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create an ezcUrlConfiguration object
- $urlCfg = new ezcUrlConfiguration();
-
- // set the basedir and script values
- $urlCfg->basedir = 'mydir';
- $urlCfg->script = 'index.php';
-
- // define delimiters for unordered parameter names
- $urlCfg->unorderedDelimiters = array( '(', ')' );
-
- // define ordered parameters
- $urlCfg->addOrderedParameter( 'section' );
- $urlCfg->addOrderedParameter( 'group' );
- $urlCfg->addOrderedParameter( 'category' );
- $urlCfg->addOrderedParameter( 'subcategory' );
-
- // define unordered parameters
- $urlCfg->addUnorderedParameter( 'game', ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
-
- // create a new ezcUrl object from a string url and use the above $urlCfg
- $url = new ezcUrl( 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7', $urlCfg );
-
- // get the parameter values from the url
- var_dump( $url->getParam( 'section' ) );
- var_dump( $url->getParam( 'group' ) );
- var_dump( $url->getParam( 'category' ) );
- var_dump( $url->getParam( 'subcategory' ) );
- var_dump( $url->getParam( 'game' ) );
-
- // output the url (index.php will not be there)
- var_dump( $url->buildUrl() );
-
- // output the url (with index.php included)
- var_dump( $url->buildUrl( true ) );
- ?>
The output would be as follows (wrapped for clarity):
string(6) "groups"
string(5) "Games"
string(9) "Adventure"
string(5) "Adult"
array(2) {
[0]=>
string(5) "Larry"
[1]=>
string(1) "7"
}
string(72) "http://www.example.com/mydir/groups/Games/Adventure/Adult/
(game)/Larry/7"
string(82) "http://www.example.com/mydir/index.php/groups/Games/Adventure/
Adult/(game)/Larry/7"
If the URL contains multiple appearances of an unordered parameter (for example
'http://www.example.com/(param1)/x/(param1)/y/z'), then by default only the last
encountered values are returned when calling getParam().
To return all the values (to aggregate values) as an array of arrays (for
example, array( array( 'x' ), array( 'y', 'z' ) ) for the above URL), use
ezcUrlConfiguration::AGGREGATE_ARGUMENTS as the second parameter when calling
addUnorderedParameter(), like in this example:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create an ezcUrlConfiguration object
- $urlCfg = new ezcUrlConfiguration();
-
- // single parameter value
- $urlCfg->addUnorderedParameter( 'param1' );
- $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z', $urlCfg );
- var_dump( $url->getParam( 'param1' ) ); // will output "y"
-
- // multiple parameter values
- $urlCfg->addUnorderedParameter( 'param1', ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
- $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z', $urlCfg );
- var_dump( $url->getParam( 'param1' ) ); // will output array( "y", "z" )
-
- // multiple parameter values with aggregation
- $urlCfg->addUnorderedParameter( 'param1', ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
- $url = new ezcUrl( 'http://www.example.com/(param1)/x/(param1)/y/z', $urlCfg );
- var_dump( $url->getParam( 'param1' ) ); // will output array( array( "x" ), array( "y", "z" ) )
-
- // output the url (it will be similar to the input url)
- var_dump( $url->buildUrl() );
- ?>
The output will be:
string(1) "y"
array(2) {
[0]=>
string(1) "y"
[1]=>
string(1) "z"
}
array(2) {
[0]=>
array(1) {
[0]=>
string(1) "x"
}
[1]=>
array(2) {
[0]=>
string(1) "y"
[1]=>
string(1) "z"
}
}
string(46) "http://www.example.com/(param1)/x/(param1)/y/z"
If the URL doesn't contain delimiters for unordered parameter names, then you
can use the getParams() method to return the values after the basedir, script
and ordered parameters as a flat array. This array can be parsed later by your
application to make it an associative array.
Example:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create an ezcUrlConfiguration object
- $urlCfg = new ezcUrlConfiguration();
-
- $urlCfg->basedir = '/mydir/shop';
- $urlCfg->script = 'index.php';
- $urlCfg->addOrderedParameter( 'module' );
-
- $url = new ezcUrl( 'http://www.example.com/mydir/shop/index.php/order/Software/PHP/Version/5.2/Extension/XDebug/Extension/openssl', $urlCfg );
-
- // get the unordered parameters as a flat array
- var_dump( $url->getParams() ); // will output array( 'Software', 'PHP', 'Version', '5.2', 'Extension', 'XDebug', 'Extension', 'openssl' )
- ?>
The output will be:
array(8) {
[0]=>
string(8) "Software"
[1]=>
string(3) "PHP"
[2]=>
string(7) "Version"
[3]=>
string(3) "5.2"
[4]=>
string(9) "Extension"
[5]=>
string(6) "XDebug"
[6]=>
string(9) "Extension"
[7]=>
string(7) "openssl"
}
This array can then be translated by your application to this form (if
needed):
array( "Software" => array( "PHP" ),
"Version" => array( "5.2" ),
"Extension" => array( "XDebug", "openssl" )
);
The following example uses the custom url configuration from before to set
the parameters into the provided url:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create an ezcUrlConfiguration object
- $urlCfg = new ezcUrlConfiguration();
-
- // set the basedir and script values
- $urlCfg->basedir = 'mydir';
- $urlCfg->script = 'index.php';
-
- // define delimiters for unordered parameter names
- $urlCfg->unorderedDelimiters = array( '(', ')' );
-
- // define ordered parameters
- $urlCfg->addOrderedParameter( 'section' );
- $urlCfg->addOrderedParameter( 'group' );
- $urlCfg->addOrderedParameter( 'category' );
- $urlCfg->addOrderedParameter( 'subcategory' );
-
- // define unordered parameters
- $urlCfg->addUnorderedParameter( 'game', ezcUrlConfiguration::MULTIPLE_ARGUMENTS );
- $urlCfg->addUnorderedParameter( 'patches', ezcUrlConfiguration::AGGREGATE_ARGUMENTS );
-
- // create a new ezcUrl object from a string url and use the above $urlCfg
- $url = new ezcUrl( 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7', $urlCfg );
- var_dump( $url->buildUrl() );
-
- // set the parameter values in the url
- $url->setParam( 'subcategory', 'Kids' );
- $url->setParam( 'game', array( 'Monkey_Island', '3' ) );
- var_dump( $url->buildUrl() );
-
- $url->setParam( 'patches', array( array( 'beta1' ), array( 'rc1', 'rc2' ) ) );
- var_dump( $url->buildUrl() );
- ?>
The output would be as follows (wrapped for clarity):
string(72) "http://www.example.com/mydir/groups/Games/Adventure/Adult/
(game)/Larry/7"
string(79) "http://www.example.com/mydir/groups/Games/Adventure/Kids/
(game)/Monkey_Island/3"
string(113) "http://www.example.com/mydir/groups/Games/Adventure/Kids/
(game)/Monkey_Island/3/(patches)/beta1/(patches)/rc1/rc2"
The following example uses the custom url configuration from before to set
the parameters into the provided url:
- <?php
- require_once 'tutorial_autoload.php';
-
- // create a default url configuration
- $urlCfgDefault = new ezcUrlConfiguration();
- $urlCfgDefault->addOrderedParameter( 'section' );
-
- // create a configuration for artists
- $urlCfgArtist = new ezcUrlConfiguration();
- $urlCfgArtist->addOrderedParameter( 'section' );
- $urlCfgArtist->addOrderedParameter( 'artist_name' );
-
- // create a configuration for albums
- $urlCfgAlbum = new ezcUrlConfiguration();
- $urlCfgAlbum->addOrderedParameter( 'section' );
- $urlCfgAlbum->addOrderedParameter( 'artist_name' );
- $urlCfgAlbum->addOrderedParameter( 'album_name' );
-
- // create a configuration for music genres
- $urlCfgGenre = new ezcUrlConfiguration();
- $urlCfgGenre->addOrderedParameter( 'section' );
- $urlCfgGenre->addOrderedParameter( 'genre_name' );
-
- $url = new ezcUrl( 'http://mymusicsite.com/showartist/Beatles', $urlCfgDefault );
-
- switch ( $url->getParam( 'section' ) )
- {
- case 'showartist':
- $url->applyConfiguration( $urlCfgArtist );
- $artist = $url->getParam( 'artist_name' );
- // do stuff with $artist
- var_dump( $artist );
- break;
- case 'showalbum':
- $url->applyConfiguration( $urlCfgAlbum );
- $artist = $url->getParam( 'artist_name' );
- $album = $url->getParam( 'album_name' );
- // do stuff with $artist and $album
- var_dump( $artist );
- var_dump( $album );
- break;
- case 'showgenre':
- $url->applyConfiguration( $urlCfgGenre );
- $genre = $url->getParam( 'genre_name' );
- // do stuff with $genre
- var_dump( $genre );
- break;
- }
-
- ?>
The output would be:
string(7) "Beatles"
With the url creator, you can register a url under an alias, then use that
alias when you want to prepend the url to a file name.
- <?php
- require_once 'tutorial_autoload.php';
-
- // register an url under the alias 'map'
- ezcUrlCreator::registerUrl( 'map', '/images/geo?xsize=450&ysize=450&zoom=4' );
-
- // display the the url prepended to map_norway.gif
- var_dump( ezcUrlCreator::prependUrl( 'map', 'map_norway.gif' ) );
-
- // display the the url prepended to map_sweden.gif
- var_dump( ezcUrlCreator::prependUrl( 'map', 'map_sweden.gif' ) );
-
- // display the stored url under the alias 'map'
- var_dump( ezcUrlCreator::getUrl( 'map' ) );
-
- ?>
The output would be:
string(53) "/images/geo/map_norway.gif?xsize=450&ysize=450&zoom=4"
string(53) "/images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4"
string(38) "/images/geo?xsize=450&ysize=450&zoom=4"
With the url creator, you can register a url under an alias, then use that
alias when you want to apply formatting to a url.
- <?php
- require_once 'tutorial_autoload.php';
-
- // register an url under the alias 'map'
- ezcUrlCreator::registerUrl( 'map', '/images/geo/%s?xsize=%d&ysize=%d&zoom=%d' );
-
- // display the stored url under the alias 'map' formatted with parameters
- var_dump( ezcUrlCreator::getUrl( 'map', 'map_norway.gif', 450, 450, 4 ) );
-
- // display the stored url under the alias 'map' formatted with other parameters
- var_dump( ezcUrlCreator::getUrl( 'map', 'map_sweden.gif', 450, 450, 4 ) );
- ?>
The output would be:
string(53) "/images/geo/map_norway.gif?xsize=450&ysize=450&zoom=4"
string(53) "/images/geo/map_sweden.gif?xsize=450&ysize=450&zoom=4"
In the class ezcUrlTools there are some useful functions for handling urls.
This function builds the current url from the $_SERVER array or another
array source. The $_SERVER array is used by default, but another array source
can be specified as a parameter when calling the function.
For example, if the $_SERVER array has these fields:
- <?php
- $_SERVER = array(
- 'HTTPS' => '1',
- 'SERVER_NAME' => 'www.example.com',
- 'SERVER_PORT' => 80,
- 'REQUEST_URI' => '/index.php'
- );
-
- $url = ezcUrlTools::getCurrentUrl();
- ?>
Then $url will be:
https://www.example.com/index.php
This function has the same functionality as the PHP function parse_str(), but
without converting the dots to underscores in query parameter names.
Example:
- <?php
- $params = ezcUrlTools::parseQueryString(
- 'openid.nonce=123456&foo[]=bar&foo[]=baz' );
- ?>
Then $params will be:
array(2) {
["openid.nonce"]=>
string(6) "123456"
["foo"]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
}
By calling parse_str() on the same string, instead of 'openid.nonce' as a key
in $params there would have been 'openid_nonce'.