<commit_before>Router Component
================

Router component is used for mapping defined paths/urls to appropriate controllers or services.

Install the component
---------------------
The best way to install the component is using Composer.

```json
{
    "require": {
        "webiny/router": "1.0.*"
    }
}
```
For additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/router).

Once you have your `composer.json` file in place, just run the install command.

    $ php composer.phar install

To learn more about Composer, and how to use it, please visit [this link](https://getcomposer.org/doc/01-basic-usage.md).

Alternatively, you can also do a `git checkout` of the repo.

## Usage

Defining a route is a rather easy process, you set a route name and underneath you define the path and the callback.
Here is an example:

```yaml
    Router:
        Routes:
            BlogTag:
                Path: blog/tag/{tag}
                Callback: MyApp\Blog\showTag
            BlogComments:
                Path: blog/post/{id}/comments
                Callback:
                    Class: MyApp\Blog
                    Method: showComments
            BlogPost:
                Path: blog/post/{slug}/{id}
                Callback: MyApp\Blog\showPost
                Options:
                    slug:
                        Pattern: .*?
            BlogAuthor:
                Path: blog/authors/{author}
                Callback: MyApp\Blog\showAuthorPosts
                Options:
                    author:
                        Default: webiny
            Blog:
                Path: blog
                Callback: MyApp\Blog\index
```

If a route is matched you will get an instance of `MatchedRoute`. The `getCallback` method returns the value of callback
parameter of the matched route. The second method `getParams` returns the values of the parameters defined in the `Path` section.

By registering a default `Router` configuration, `Router` will automatically set the defined routes and cache driver.
```php
    Router::setConfig($pathToYourConfig);
```

Later on you can use the `RouterTrait` access that `Router` instance.

**NOTICE:**
When defining the routes in your config file, make sure that you put routes with lesser precision to the bottom, and
routes with a more stricter rules at the top. A common pitfall is that a route with more wider pattern is matched before
a route with a more stricter rules due to the fact that the `Router` loops over the defined routes in the same order
that they are defined inside the config file, and it stops once the first route is matched.

### `Options`

The `Options` attribute under the route provides you two additional options that you can set under each route.

#### `Pattern`

The default regex pattern for matching a variable is `\{\w+\}`. Using the `Pattern` attribute you can set your own rule.

#### `Default`

The `Default` attribute gives you the option to set the the default value for a variable.

If the route is not matched using the current available properties from the url path, `Router` will replace all the<commit_msg>Updated the default pattern inside the readme.<commit_after>Router Component
================

Router component is used for mapping defined paths/urls to appropriate controllers or services.

Install the component
---------------------
The best way to install the component is using Composer.

```json
{
    "require": {
        "webiny/router": "1.0.*"
    }
}
```
For additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/router).

Once you have your `composer.json` file in place, just run the install command.

    $ php composer.phar install

To learn more about Composer, and how to use it, please visit [this link](https://getcomposer.org/doc/01-basic-usage.md).

Alternatively, you can also do a `git checkout` of the repo.

## Usage

Defining a route is a rather easy process, you set a route name and underneath you define the path and the callback.
Here is an example:

```yaml
    Router:
        Routes:
            BlogTag:
                Path: blog/tag/{tag}
                Callback: MyApp\Blog\showTag
            BlogComments:
                Path: blog/post/{id}/comments
                Callback:
                    Class: MyApp\Blog
                    Method: showComments
            BlogPost:
                Path: blog/post/{slug}/{id}
                Callback: MyApp\Blog\showPost
                Options:
                    slug:
                        Pattern: .*?
            BlogAuthor:
                Path: blog/authors/{author}
                Callback: MyApp\Blog\showAuthorPosts
                Options:
                    author:
                        Default: webiny
            Blog:
                Path: blog
                Callback: MyApp\Blog\index
```

If a route is matched you will get an instance of `MatchedRoute`. The `getCallback` method returns the value of callback
parameter of the matched route. The second method `getParams` returns the values of the parameters defined in the `Path` section.

By registering a default `Router` configuration, `Router` will automatically set the defined routes and cache driver.
```php
    Router::setConfig($pathToYourConfig);
```

Later on you can use the `RouterTrait` access that `Router` instance.

**NOTICE:**
When defining the routes in your config file, make sure that you put routes with lesser precision to the bottom, and
routes with a more stricter rules at the top. A common pitfall is that a route with more wider pattern is matched before
a route with a more stricter rules due to the fact that the `Router` loops over the defined routes in the same order
that they are defined inside the config file, and it stops once the first route is matched.

### `Options`

The `Options` attribute under the route provides you two additional options that you can set under each route.

#### `Pattern`

The default regex pattern for matching a variable is `{[\w-]+}`. Using the `Pattern` attribute you can set your own rule.

#### `Default`

The `Default` attribute gives you the option to set the the default value for a variable.

If the route is not matched using the current available properties from the url path, `Router` will replace all the
{'fraction_non_alphanumeric': 0.08120868744098206, 'fraction_numerical': 0.0012590494176896443, 'mean_word_length': 2.4707809939923537, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6524427', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1533, 'n_words': 763}