Development

Changeset 8281

You must first sign up to be able to contribute.

Changeset 8281

Show
Ignore:
Timestamp:
04/04/08 14:13:29 (8 months ago)
Author:
francois
Message:

[doc 1.1] documented changes in the routing system

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.1/book/09-Links-and-the-Routing-System.txt

    r8255 r8281  
    435435Patterns can contain wildcards (represented by an asterisk, *) and named wildcards (starting with a colon, :). A match to a named wildcard becomes a request parameter value. For instance, the `default` rule defined in Listing 9-15 will match any URL like `/foo/bar`, and set the `module` parameter to `foo` and the `action` parameter to `bar`. And in the `default_symfony` rule, `symfony` is a keyword and `action` is named wildcard parameter. 
    436436 
     437>**NOTE** 
     438>**New in symfony 1.1** named wildcards can be separated by a slash or a dot, so you can write a pattern like: 
     439> 
     440>    my_rule: 
     441>      url:   /foo/:bar.:format 
     442>      param: { module: mymodule, action: myaction } 
     443> 
     444>That way, an external URL like 'foo/12.xml' will math `my_rule` and execute `mymodule/muaction` with two parameters: `$bar=12` and `$format=xml`. 
     445>You can add more separators by changing the `segment_separators` parameters value in the `sfPatternRouting` factory configuration (see chapter 19). 
     446 
    437447The routing system parses the `routing.yml` file from the top to the bottom and stops at the first match. This is why you must add your own rules on top of the default ones. For instance, the URL `/foo/123` matches both of the rules defined in Listing 9-16, but symfony first tests `my_rule:`, and as that rule matches, it doesn't even test the `default:` one. The request is handled by the `mymodule/myaction` action with `bar` set to `123` (and not by the `foo/123` action). 
    438448 
     
    551561### Speeding Up Routing by Using the Rule Name 
    552562 
    553 The link helpers accept a rule label instead of a module/action pair if the rule label is preceded by an at sign (@), as shown in Listing 9-21. 
     563The link helpers accept a rule label instead of a module/action pair if the rule label is preceded by an 'at' sign (@), as shown in Listing 9-21. 
    554564 
    555565Listing 9-21 - Using the Rule Label Instead of the Module/Action 
     
    563573There are pros and cons to this trick. The advantages are as follows: 
    564574 
    565   * The formatting of internal URIs is done much faster, since symfony doesn't have to browse all the rules to find the one that matches the link. In a page with a great number of routed hyperlinks, the boost will be noticeable if you use rule labels instead of module/action pairs. 
     575  * The formatting of internal URIs is done faster, since symfony doesn't have to browse all the rules to find the one that matches the link. In a page with a great number of routed hyperlinks, the boost will be noticeable if you use rule labels instead of module/action pairs. 
    566576  * Using the rule label helps to abstract the logic behind an action. If you decide to change an action name but keep the URL, a simple change in the `routing.yml` file will suffice. All of the `link_to()` calls will still work without further change. 
    567577  * The logic of the call is more apparent with a rule name. Even if your modules and actions have explicit names, it is often better to call `@display_article_by_slug` than `article/display`. 
     
    573583>**TIP** 
    574584>During your tests (in the `dev` environment), if you want to check which rule was matched for a given request in your browser, develop the "logs and msgs" section of the web debug toolbar and look for a line specifying "matched route XXX." You will find more information about the web debug mode in Chapter 16. 
     585 
     586>**NOTE** 
     587>**New in symfony 1.1** Routing operations are much faster in production mode, where conversions between external URLs and internal URIs are cached. 
    575588 
    576589### Adding an .html Extension 
     
    624637    ); 
    625638 
    626 The sfRouting singleton has other useful methods for handling routes by hand: `clearRoutes()`, `hasRoutes()` and so on. Refer to the API documentation ([http://www.symfony-project.org/api/symfony.html](http://www.symfony-project.org/api/symfony.html)) to learn more. 
     639The sfRouting class has other useful methods for handling routes by hand: `clearRoutes()`, `hasRoutes()` and so on. Refer to the API documentation ([http://www.symfony-project.org/api/symfony.html](http://www.symfony-project.org/api/symfony.html)) to learn more. 
    627640 
    628641>**TIP** 
  • doc/branches/1.1/book/19-Mastering-Symfony-s-Configuration-Files.txt

    r8266 r8281  
    1212 
    1313### Default Modules and Actions 
    14  
    15 When a routing rule doesn't define the `module` or the `action` parameter, values from the `settings.yml` file are used instead: 
    16  
    17   * `default_module`: Default `module` request parameter. Defaults to the `default` module. 
    18   * `default_action`: Default `action` request parameter. Defaults to the `index` action. 
    1914 
    2015Symfony provides default pages for special situations. In the case of a routing error, symfony executes an action of the `default` module, which is stored in the `$sf_symfony_data_dir/modules/default/` directory. The `settings.yml` file defines which action is executed depending on the error: 
     
    5651`use_database`          | Enables the database manager. Set it to `off` if you don't use a database. | `on` 
    5752`use_security`          | Enables security features (secure actions and credentials; see Chapter 6). The default security filter (`sfBasicSecurityFilter`) is enabled only if it is `on`. | `on` 
    58 `use_flash`             | Enables the flash parameter feature (see Chapter 6). Set it to `off` if you never use the `setFlash()` method of sfUser. | `on` 
    5953`i18n`                  | Enables interface translation (see Chapter 13). Set it to `on` for multilingual applications. | `off` 
    6054`logging_enabled`       | Enables logging of symfony events. Set it to off when you want to ignore the logging.yml settings and turn symfony logging off completely. | `on` 
     
    8074#### Routing Settings 
    8175 
    82 Two routing settings (see Chapter 9) are stored in `settings.yml`: 
     76The routing settings (see Chapter 9) are defined in `factories.yml`, under the `routing` key. Listing 19-1 show the default routing configuration. 
     77 
     78Listing 19-1 - Routing Configuration Settings, in `frontend/config/factories.yml` 
     79 
     80    routing: 
     81      class: sfPatternRouting 
     82      param: 
     83        load_configuration: true 
     84        suffix:             . 
     85        default_module:     default 
     86        default_action:     index 
     87        variable_prefixes:  [':'] 
     88        segment_separators: ['/', '.'] 
     89        variable_regex:     '[\w\d_]+' 
     90        debug:              %SF_DEBUG% 
     91        logging:            %SF_LOGGING_ENABLED% 
     92        cache: 
     93          class: sfFileCache 
     94          param: 
     95            automatic_cleaning_factor: 0 
     96            cache_dir:                 %SF_CONFIG_CACHE_DIR%/routing 
     97            lifetime:                  31556926 
     98            prefix:                    %SF_APP_DIR% 
    8399 
    84100  * The `suffix` parameter sets the default suffix for generated URLs. The default value is a period (`.`), and it corresponds to no suffix. Set it to `.html`, for instance, to have all generated URLs look like static pages. 
    85   * The `no_script_name` parameter enables the front controller name in generated URLs. The `no_script_name` setting can be on only for a single application in a project, unless you store the front controllers in various directories and alter the default URL rewriting rules. It is usually on for the production environment of your main application and off for the others. 
     101  * When a routing rule doesn't define the `module` or the `action` parameter, values from the `factories.yml` are used instead: 
     102    * `default_module`: Default `module` request parameter. Defaults to the `default` module. 
     103    * `default_action`: Default `action` request parameter. Defaults to the `index` action. 
     104  * By default, route patterns identify named wildcards by a colon (`:`) prefix. But if you want to write your rules in a more PHP-friendly syntax, you can add the dollar (`$`) sign in the `variable_prefixes` array. That way, you can write a pattern like '/article/$year/$month/$day/$title' instead of '/article/:year/:month/:day/:title'. 
     105  * The pattern routing will identify named wildcards between separators. The default separators are the slash and the dot, but you can add more if you want in the `segment_separators` parameter. For instance, if you add the dash (`-`), you can write a pattern like '/article/:year-:month-:day/:title'. 
     106  * The pattern routing uses its own cache, in production mode, to speed up conversions between external URLs and internal URIs. By default, this cache uses the filesystem, but you can use any cache class, provided that you declare the class and its settings in the `cache` parameter. See Chapter 15 for the list of available cache storage classes. To deactivate the routing cache in production, set the `debug` parameter to `on`. 
     107 
     108There is one additional parameter related to routing, but this one is stored in `settings.yml`: 
     109  * `no_script_name` enables the front controller name in generated URLs. The `no_script_name` setting can be on only for a single application in a project, unless you store the front controllers in various directories and alter the default URL rewriting rules. It is usually on for the production environment of your main application and off for the others. 
    86110 
    87111#### Form Validation Settings 
     
    126150#### Miscellaneous Configuration 
    127151 
    128 The `settings.yml` file contains a few more parameters, used internally by symfony for core behaviors. Listing 19-1 lists them as they appear in the configuration file. 
    129  
    130 Listing 19-1 - Miscellaneous Configuration Settings, in `frontend/config/settings.yml` 
     152The `settings.yml` file contains a few more parameters, used internally by symfony for core behaviors. Listing 19-2 lists them as they appear in the configuration file. 
     153 
     154Listing 19-2 - Miscellaneous Configuration Settings, in `frontend/config/settings.yml` 
    131155 
    132156    # Remove comments in core framework classes as defined in the core_compile.yml 
     
    172196There is no `autoload.yml` file in the default application configuration directory. If you want to modify the framework settings--for instance, to autoload classes stored somewhere else in your file structure--create an empty autoload.yml file and override the settings of `$sf_symfony_data_dir/config/autoload.yml` or add your own. 
    173197 
    174 The autoload.yml file must start with an autoload: key and list the locations where symfony should look for classes. Each location requires a label; this gives you the ability to override symfony's entries. For each location, provide a `name` (it will appear as a comment in `config_autoload.yml.php`) and an absolute `path`. Then define if the search must be `recursive`, which directs symfony to look in all the subdirectories for `.php` files, and `exclude` the subdirectories you want. Listing 19-2 shows the locations used by default and the file syntax. 
    175  
    176 Listing 19-2 - Default Autoloading Configuration, in `$sf_symfony_data_dir/config/autoload.yml` 
     198The autoload.yml file must start with an autoload: key and list the locations where symfony should look for classes. Each location requires a label; this gives you the ability to override symfony's entries. For each location, provide a `name` (it will appear as a comment in `config_autoload.yml.php`) and an absolute `path`. Then define if the search must be `recursive`, which directs symfony to look in all the subdirectories for `.php` files, and `exclude` the subdirectories you want. Listing 19-3 shows the locations used by default and the file syntax. 
     199 
     200Listing 19-3 - Default Autoloading Configuration, in `$sf_symfony_data_dir/config/autoload.yml` 
    177201 
    178202    autoload: 
     
    268292### The Basic File Structure 
    269293 
    270 The path variables are defined in the `$sf_symfony_data_dir/config/constants.php` file, included when the application bootstraps. These variables are stored in the `sfConfig` object, and so they are easy to override. Listing 19-3 shows a listing of the path variables and the directory they reference. 
    271  
    272 Listing 19-3 - Default File Structure Variables, from `$sf_symfony_data_dir/config/constants.php` 
     294The path variables are defined in the `$sf_symfony_data_dir/config/constants.php` file, included when the application bootstraps. These variables are stored in the `sfConfig` object, and so they are easy to override. Listing 19-4 shows a listing of the path variables and the directory they reference. 
     295 
     296Listing 19-4 - Default File Structure Variables, from `$sf_symfony_data_dir/config/constants.php` 
    273297 
    274298    sf_root_dir           # myproject/ 
     
    302326Every path to a key directory is determined by a parameter ending with `_dir`. Always use the path variables instead of real (relative or absolute) file paths, so that you will be able to change them later, if necessary. For instance, when you want to move a file to the `uploads/` directory in an application, you should use `sfConfig::get('sf_upload_dir')` for the path instead of `SF_ROOT_DIR.'/web/uploads/'`. 
    303327 
    304 The module directory structure is defined at runtime, when the routing system determines the module name (`$module_name`). It is automatically built according to the path names defined in the `constants.php` file, as shown in Listing 19-4
    305  
    306 Listing 19-4 - Default Module File Structure Variables 
     328The module directory structure is defined at runtime, when the routing system determines the module name (`$module_name`). It is automatically built according to the path names defined in the `constants.php` file, as shown in Listing 19-5
     329 
     330Listing 19-5 - Default Module File Structure Variables 
    307331 
    308332    sf_app_module_dir                 # modules/ 
     
    337361### Modifying the Project Web Root 
    338362 
    339 All the paths built in `constants.php` rely on the project root directory, which is a constant defined in the front controller (`SF_ROOT_DIR`). Usually, the root directory is one level above the `web/` directory, but you can use a different structure. Suppose that your main directory structure is made of two directories, one public and one private, as shown in Listing 19-5. This typically happens when hosting a project on a shared host. 
    340  
    341 Listing 19-5 - Example of Custom Directory Structure for a Shared Host 
     363All the paths built in `constants.php` rely on the project root directory, which is a constant defined in the front controller (`SF_ROOT_DIR`). Usually, the root directory is one level above the `web/` directory, but you can use a different structure. Suppose that your main directory structure is made of two directories, one public and one private, as shown in Listing 19-6. This typically happens when hosting a project on a shared host. 
     364 
     365Listing 19-6 - Example of Custom Directory Structure for a Shared Host 
    342366 
    343367    symfony/    # Private area 
     
    367391### Linking to Symfony Libraries 
    368392 
    369 The paths to the framework files are defined in the project `config.php` file, as you can see in Listing 19-6
    370  
    371 Listing 19-6 - The Paths to the Framework Files, in `myproject/config/config.php` 
     393The paths to the framework files are defined in the project `config.php` file, as you can see in Listing 19-7
     394 
     395Listing 19-7 - The Paths to the Framework Files, in `myproject/config/config.php` 
    372396 
    373397    [php] 
     
    419443### Default Configuration Handlers 
    420444 
    421 The default handler configuration is stored in `$sf_symfony_data_dir/config/config_handlers.yml`. This file links the handlers to the configuration files according to a file path. Listing 19-7 shows an extract of this file. 
    422  
    423 Listing 19-7 - Extract of `$sf_symfony_data_dir/config/config_handlers.yml` 
     445The default handler configuration is stored in `$sf_symfony_data_dir/config/config_handlers.yml`. This file links the handlers to the configuration files according to a file path. Listing 19-8 shows an extract of this file. 
     446 
     447Listing 19-8 - Extract of `$sf_symfony_data_dir/config/config_handlers.yml` 
    424448 
    425449    config/settings.yml: 
     
    462486If you feel like writing your own configuration handler, follow the example of the structure used by the framework in the `$sf_symfony_lib_dir/config/` directory. 
    463487 
    464 Let's suppose that your application contains a `myMapAPI` class, which provides an interface to a third-party web service delivering maps. This class needs to be initialized with a URL and a user name, as shown in Listing 19-8
    465  
    466 Listing 19-8 - Example of Initialization of the `myMapAPI` Class 
     488Let's suppose that your application contains a `myMapAPI` class, which provides an interface to a third-party web service delivering maps. This class needs to be initialized with a URL and a user name, as shown in Listing 19-9
     489 
     490Listing 19-9 - Example of Initialization of the `myMapAPI` Class 
    467491 
    468492    [php] 
     
    477501      user: foobar 
    478502 
    479 In order to transform these settings into code equivalent to Listing 19-8, you must build a configuration handler. Each configuration handler must extend `sfConfigHandler` and provide an `execute()` method, which expects an array of file paths to configuration files as a parameter, and must return data to be written in a cache file. Handlers for YAML files should extend the `sfYamlConfigHandler` class, which provides additional facilities for YAML parsing. For the `map.yml` file, a typical configuration handler could be written as shown in Listing 19-9
    480  
    481 Listing 19-9 - A Custom Configuration Handler, in `frontend/lib/myMapConfigHandler.class.php` 
     503In order to transform these settings into code equivalent to Listing 19-8, you must build a configuration handler. Each configuration handler must extend `sfConfigHandler` and provide an `execute()` method, which expects an array of file paths to configuration files as a parameter, and must return data to be written in a cache file. Handlers for YAML files should extend the `sfYamlConfigHandler` class, which provides additional facilities for YAML parsing. For the `map.yml` file, a typical configuration handler could be written as shown in Listing 19-10
     504 
     505Listing 19-10 - A Custom Configuration Handler, in `frontend/lib/myMapConfigHandler.class.php` 
    482506 
    483507    [php] 
     
    551575------------------------ 
    552576 
    553 In order to have a PHP environment compatible with the rules and best practices of agile development, symfony checks and overrides a few settings of the `php.ini` configuration. This is the purpose of the `php.yml` file. Listing 19-10 shows the default `php.yml` file. 
    554  
    555 Listing 19-10 - Default PHP Settings for Symfony, in `$sf_symfony_data_dir/config/php.yml` 
     577In order to have a PHP environment compatible with the rules and best practices of agile development, symfony checks and overrides a few settings of the `php.ini` configuration. This is the purpose of the `php.yml` file. Listing 19-11 shows the default `php.yml` file. 
     578 
     579Listing 19-11 - Default PHP Settings for Symfony, in `$sf_symfony_data_dir/config/php.yml` 
    556580 
    557581    set: