# Routing


Flare can collect information about the routing of your application. This includes:

- The routing process to find a route to a controller
- Middleware that is executed

This functionality is enabled by default, but you can disable it by calling `ignoreRequests()` on the Flare config:

```php
$config->ignoreRequests();
```

## Routing stages

Flare defines five routing stages:

- Global before middleware: middleware executed for every route within your application on the request
- Routing: the process of finding a route to a controller
- Before middleware: middleware executed for the current route on the request
- After middleware: middleware executed for the current route on the response
- Global after middleware: middleware executed for every route within your application on the response

### Mixing and matching stages

You do not have to record all five stages. Record only the ones your framework actually has, and skip the rest. A few valid combinations:

- Routing, before middleware, after middleware (no global middleware concept).
- Just routing (no middleware at all).
- Just a before middleware stage (a router that resolves the handler later, with only request-side middleware).
- Global before middleware, routing, global after middleware (global middleware but no per-route middleware).

The only rule is that the stages you do record must follow the order shown above: global before middleware, then routing, then before middleware, then after middleware, then global after middleware. The recorder enforces this automatically by closing any open earlier stage when you start a later one, so a stray ordering mistake will not corrupt your trace, but skipping backwards (recording an earlier stage after a later one has already started) is silently ignored.

## Recording routing stages

We cannot automatically record the routing stages in our framework-agnostic version of the Flare client. You can do this manually as such:

**Global before middleware**

```php
$flare->routing()->recordGlobalBeforeMiddlewareStart(time: 10);

//middleware executed for every route within your application on the request

$flare->routing()->recordGlobalBeforeMiddlewareEnd(time: 20);
```

When you don't have a specific start point for this stage, you can call the following at the end of the stage:

```php
$flare->routing()->recordGlobalBeforeMiddleware(start: 10, end: 20);
```

**Routing**


```php
$flare->routing()->recordRoutingStart(time: 30);

// The process of finding a route to a controller

$flare->routing()->recordRoutingEndFromDefined(
    route: '/users/{userId}',
    method: 'GET',
    handlerName: UsersController::class,
    time: 40,
);
```

`recordRoutingEnd` (and the `recordRoutingEndFromDefined` helper) does two things in addition to closing the routing span. It fills in the [entry point](/docs/protocol/general/entry-points) handler attributes from the route, and it triggers the sampler to re-evaluate so route-based [dynamic sampling rules](/docs/php/performance/sampling) can fire now that the handler is known.

If you have a custom `RouteAttributesProvider`, pass it to `recordRoutingEnd` directly:

```php
$flare->routing()->recordRoutingEnd(
    new YourRouteAttributesProvider($route),
    time: 40,
);
```

When you don't have a specific start point for this stage, you can call the following at the end of the stage:

```php
$flare->routing()->recordRouting(start: 30, end: 40);
```

**Before middleware**

```php
$flare->routing()->recordBeforeMiddlewareStart(time: 50);

//middleware executed for the current route on the request

$flare->routing()->recordBeforeMiddlewareEnd(time: 60);
```

When you don't have a specific start point for this stage, you can call the following at the end of the stage:

```php
$flare->routing()->recordBeforeMiddleware(start: 50, end: 60);
```

**After middleware**

```php
$flare->routing()->recordAfterMiddlewareStart(time: 70);

//middleware executed for the current route on the response

$flare->routing()->recordAfterMiddlewareEnd(time: 80);
```

When you don't have a specific start point for this stage, you can call the following at the end of the stage:

```php
$flare->routing()->recordAfterMiddleware(start: 70, end: 80);
```

**Global after middleware**

```php
$flare->routing()->recordGlobalAfterMiddlewareStart(time: 90);

//middleware executed for every route within your application on the response
$flare->routing()->recordGlobalAfterMiddlewareEnd(time: 100);
```

When you don't have a specific start point for this stage, you can call the following at the end of the stage:

```php
$flare->routing()->recordGlobalAfterMiddleware(start: 90, end: 100);
```
