The following example creates a custom url configuration and uses it when
creating a new url object:
1. <?php
2. require_once 'tutorial_autoload.php';
3.
4. // create an ezcUrlConfiguration object
5. $urlCfg = new ezcUrlConfiguration();
6.
7. // set the basedir and script values
8. $urlCfg->basedir = 'mydir';
9. $urlCfg->script = 'index.php';
10.
11. // define delimiters for unordered parameter names
12. $urlCfg->unorderedDelimiters = array( '(', ')' );
13.
14. // define ordered parameters
15. $urlCfg->addOrderedParameter( 'section' );
16. $urlCfg->addOrderedParameter( 'group' );
17. $urlCfg->addOrderedParameter( 'category' );
18. $urlCfg->addOrderedParameter( 'subcategory' );
19.
20. // define unordered parameters
21. $urlCfg->addUnorderedParameter( 'game' );
22.
23. // visualize the $urlCfg object
24. var_dump( $urlCfg );
25.
26. // create a new ezcUrl object from a string url and use the above $urlCfg
27. $url = new ezcUrl( 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7', $urlCfg );
28.
29. ?>
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.
1. <?php
2. require_once 'tutorial_autoload.php';
3.
4. class customLazyUrlConfiguration implements ezcBaseConfigurationInitializer
5. {
6. public static function configureObject( $urlCfg )
7. {
8. // create an ezcUrlConfiguration object
9. $urlCfg = new ezcUrlConfiguration();
10.
11. // set the basedir and script values
12. $urlCfg->basedir = 'mydir';
13. $urlCfg->script = 'index.php';
14.
15. // define delimiters for unordered parameter names
16. $urlCfg->unorderedDelimiters = array( '(', ')' );
17.
18. // define ordered parameters
19. $urlCfg->addOrderedParameter( 'section' );
20. $urlCfg->addOrderedParameter( 'group' );
21. $urlCfg->addOrderedParameter( 'category' );
22. $urlCfg->addOrderedParameter( 'subcategory' );
23.
24. // define unordered parameters
25. $urlCfg->addUnorderedParameter( 'game' );
26. }
27. }
28.
29. ezcBaseInit::setCallback(
30. 'ezcUrlConfiguration',
31. 'customLazyUrlConfiguration'
32. );
33.
34. // Classes loaded and configured on first request
35. $url = new ezcUrl(
36. 'http://www.example.com/mydir/index.php/groups/Games/Adventure/Adult/(game)/Larry/7',
37. ezcUrlConfiguration::getInstance()
38. );
39. ?>
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.