<commit_before># Route Result Observers

Occasionally, you may have need of the `RouteResult` within other application
code. As a primary example, a URI generator may want this information to allow
creating "self" URIs, or to allow presenting a subset of parameters to generate
a URI.

Consider this URI:

```php
'/api/v{version:\d+}/post/{post_id:\d+}/comment/{comment_id:\d+}'
```

If you wanted to generate URIs to a list of related comments, you may not want
to pass the `$version` and `$post_id` parameters each and every time, but
instead just the `$comment_id`. As such, *route result observers* exist to allow
you to notify such utilities of the results of matching.

## RouteResultObserverInterface

Route result observers must implement the `RouteResultObserverInterface`:

```php
namespace Zend\Expressive;

use Zend\Expressive\Router\RouteResult;

interface RouteResultObserverInterface
{
    /**
     * Observe a route result.
     *
     * @param RouteResult $result
     */
    public function update(RouteResult $result);
}
```

These can then be attached to the `Application` instance:

```php
$app->attachRouteResultObserver($observer);
```

As noted, the observer receives the `RouteResult` from attempting to match a
route.

You can detach an existing observer as well, by passing its instance to the
`detachRouteResultObserver()` method:

```php
$app->detachRouteResultObserver($observer);
```

## Example

For this example, we'll build a simple URI generator. It will compose a
`RouterInterface` implementation, implement `RouteResultObserverInterface`, and,
when invoked, generate a URI.

```php
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\RouteResultObserverInterface;

class UriGenerator implements RouteResultObserverInterface
{
    private $defaults = [];

    private $routeName;

    private $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function update(RouteResult $result)
    {
        if ($result->isFailure()) {
            return;
        }

        $this->routeName = $result->getMatchedRouteName();
        $this->defaults  = $result->getMatchedParams();
    }

    public function __invoke($route = null, array $params = [])
    {<commit_msg>Updated documentation to reference correct namespace<commit_after># Route Result Observers

Occasionally, you may have need of the `RouteResult` within other application
code. As a primary example, a URI generator may want this information to allow
creating "self" URIs, or to allow presenting a subset of parameters to generate
a URI.

Consider this URI:

```php
'/api/v{version:\d+}/post/{post_id:\d+}/comment/{comment_id:\d+}'
```

If you wanted to generate URIs to a list of related comments, you may not want
to pass the `$version` and `$post_id` parameters each and every time, but
instead just the `$comment_id`. As such, *route result observers* exist to allow
you to notify such utilities of the results of matching.

## RouteResultObserverInterface

Route result observers must implement the `RouteResultObserverInterface`:

```php
namespace Zend\Expressive\Router;

use Zend\Expressive\Router\RouteResult;

interface RouteResultObserverInterface
{
    /**
     * Observe a route result.
     *
     * @param RouteResult $result
     */
    public function update(RouteResult $result);
}
```

These can then be attached to the `Application` instance:

```php
$app->attachRouteResultObserver($observer);
```

As noted, the observer receives the `RouteResult` from attempting to match a
route.

You can detach an existing observer as well, by passing its instance to the
`detachRouteResultObserver()` method:

```php
$app->detachRouteResultObserver($observer);
```

> ### RouteResultSubjectInterface
>
> `Zend\Expressive\Application` implements `Zend\Expressive\Router\RouteResultSubjectInterface`,
> which defines methods for attaching and detaching route result observers, as
> well as a method for notifying observers. Typically you'll only see the
> `Application` class as an implementation of the interface, but you can always
> create your own implementations as well if desired &mdash; for instance, if
> you are implementing your own middleware runtime using the various interfaces
> Expressive provides.

## Example

For this example, we'll build a simple URI generator. It will compose a
`RouterInterface` implementation, implement `RouteResultObserverInterface`, and,
when invoked, generate a URI.

```php
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\ROuter\RouteResultObserverInterface;

class UriGenerator implements RouteResultObserverInterface
{
    private $defaults = [];

    private $routeName;

    private $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function update(RouteResult $result)
    {
        if ($result->isFailure()) {
            return;
        }

        $this->routeName = $result->getMatchedRouteName();
        $this->defaults  = $result->getMatchedParams();
    }

    public function __invoke($route = null, array $params = [])
    {
{'fraction_non_alphanumeric': 0.09280122207370632, 'fraction_numerical': 0.0, 'mean_word_length': 4.323170731707317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, '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': '4133618', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1361, 'n_words': 531}