# RouteDefinition

> Immutable, persistence-safe route metadata shared by framework adapters.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [Panel](/docs/extensions/debug-core/reference/panel/index.md) › [Request](/docs/extensions/debug-core/reference/panel/request/index.md) › [Routing](/docs/extensions/debug-core/reference/panel/request/routing/index.md) › RouteDefinition

| final class | `PHPForge\Debug\Panel\Request\Routing\RouteDefinition` |
| --- | --- |
| Source | [`src/Panel/Request/Routing/RouteDefinition.php`](https://github.com/php-forge/debug-core/blob/main/src/Panel/Request/Routing/RouteDefinition.php#L24) |

Immutable, persistence-safe route metadata shared by framework adapters.

A `null` optional value means the adapter cannot expose that concept. An empty methods or hosts list means the route
is unrestricted, while an empty middleware list means the route has no middleware.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates persistence-safe route metadata. | `RouteDefinition` |
| [`create()`](#method-create) static | Creates a route definition ready for immutable enrichment. | `RouteDefinition` |
| [`fromArray()`](#method-fromArray) static | Restores a route definition from its strict scalar persistence shape. | `RouteDefinition` |
| [`getAction()`](#method-getAction) | Returns the handler the route dispatches to. | `RouteDefinition` |
| [`getHosts()`](#method-getHosts) | Returns the hosts the route is restricted to. | `RouteDefinition` |
| [`getMethods()`](#method-getMethods) | Returns the HTTP methods the route accepts. | `RouteDefinition` |
| [`getMiddlewares()`](#method-getMiddlewares) | Returns the middleware chain applied to the route. | `RouteDefinition` |
| [`getMode()`](#method-getMode) | Returns the parsing mode the rule runs in. | `RouteDefinition` |
| [`getName()`](#method-getName) | Returns the route name. | `RouteDefinition` |
| [`getPattern()`](#method-getPattern) | Returns the URL pattern the route matches. | `RouteDefinition` |
| [`getSuffix()`](#method-getSuffix) | Returns the URL suffix the rule appends. | `RouteDefinition` |
| [`getTarget()`](#method-getTarget) | Returns the route the rule resolves to. | `RouteDefinition` |
| [`getType()`](#method-getType) | Returns the rule class or category. | `RouteDefinition` |
| [`toArray()`](#method-toArray) | Returns the compact route persistence shape used in Request snapshots. | `RouteDefinition` |
| [`withAction()`](#method-withAction) | Returns a copy carrying another action. | `RouteDefinition` |
| [`withHosts()`](#method-withHosts) | Returns a copy restricted to another set of hosts. | `RouteDefinition` |
| [`withMethods()`](#method-withMethods) | Returns a copy accepting another set of HTTP methods. | `RouteDefinition` |
| [`withMiddlewares()`](#method-withMiddlewares) | Returns a copy carrying another middleware chain. | `RouteDefinition` |
| [`withMode()`](#method-withMode) | Returns a copy carrying another mode. | `RouteDefinition` |
| [`withSuffix()`](#method-withSuffix) | Returns a copy carrying another suffix. | `RouteDefinition` |
| [`withTarget()`](#method-withTarget) | Returns a copy carrying another target. | `RouteDefinition` |
| [`withType()`](#method-withType) | Returns a copy carrying another type. | `RouteDefinition` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(string $name = '', string $pattern = '')
```

Creates persistence-safe route metadata.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Route name as the application declares it. |
| `$pattern` | `string` | URL pattern the route matches. |

### create() <span id="method-create"></span>

```php
public static function create(string $name = '', string $pattern = ''): self
```

Creates a route definition ready for immutable enrichment.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Route name as the application declares it. |
| `$pattern` | `string` | URL pattern the route matches. |

**Returns**

`self`: Definition carrying only the route identity.

### fromArray() <span id="method-fromArray"></span>

```php
public static function fromArray(array<array-key, mixed> $data): self
```

Restores a route definition from its strict scalar persistence shape.

The six Yii 3 fields are required for compatibility with existing captures. Yii 2-specific fields remain
optional so old snapshots keep hydrating without schema migration.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array<array-key, mixed>` | Persisted route definition. |

**Returns**

`self`: Definition restored from the persisted shape.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): when a required field is missing, a field is declared but unknown, or a value does not match its persisted type.

### getAction() <span id="method-getAction"></span>

```php
public function getAction(): string|null
```

Returns the handler the route dispatches to.

**Returns**

`string|null`: Handler the route dispatches to, or `null` when unsupported.

### getHosts() <span id="method-getHosts"></span>

```php
public function getHosts(): list<string>
```

Returns the hosts the route is restricted to.

**Returns**

`list<string>`: Hosts the route is restricted to; empty when it is unrestricted.

### getMethods() <span id="method-getMethods"></span>

```php
public function getMethods(): list<string>
```

Returns the HTTP methods the route accepts.

**Returns**

`list<string>`: HTTP methods the route accepts; empty when it accepts any.

### getMiddlewares() <span id="method-getMiddlewares"></span>

```php
public function getMiddlewares(): list<string>|null
```

Returns the middleware chain applied to the route.

**Returns**

`list<string>|null`: Middleware labels applied to the route; empty when it has none, or `null` when unsupported.

### getMode() <span id="method-getMode"></span>

```php
public function getMode(): string|null
```

Returns the parsing mode the rule runs in.

**Returns**

`string|null`: Parsing mode the rule runs in, or `null` when unsupported.

### getName() <span id="method-getName"></span>

```php
public function getName(): string
```

Returns the route name.

**Returns**

`string`: Route name as the application declares it.

### getPattern() <span id="method-getPattern"></span>

```php
public function getPattern(): string
```

Returns the URL pattern the route matches.

**Returns**

`string`: URL pattern the route matches.

### getSuffix() <span id="method-getSuffix"></span>

```php
public function getSuffix(): string|null
```

Returns the URL suffix the rule appends.

**Returns**

`string|null`: URL suffix the rule appends, or `null` when unsupported.

### getTarget() <span id="method-getTarget"></span>

```php
public function getTarget(): string|null
```

Returns the route the rule resolves to.

**Returns**

`string|null`: Route the rule resolves to, or `null` when unsupported.

### getType() <span id="method-getType"></span>

```php
public function getType(): string|null
```

Returns the rule class or category.

**Returns**

`string|null`: Rule class or category reported by the adapter, or `null` when unsupported.

### toArray() <span id="method-toArray"></span>

```php
public function toArray(): array{name: string, pattern: string, methods: list<string>, hosts: list<string>, action: string|null, middlewares: list<string>|null, target?: string, suffix?: string, mode?: string, type?: string}
```

Returns the compact route persistence shape used in Request snapshots.

**Returns**

`array{name: string, pattern: string, methods: list<string>, hosts: list<string>, action: string|null, middlewares: list<string>|null, target?: string, suffix?: string, mode?: string, type?: string}`: Route fields; the optional ones appear only when the adapter exposed them.

### withAction() <span id="method-withAction"></span>

```php
public function withAction(string|null $action): self
```

Returns a copy carrying another action.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | `string\|null` | Handler the route dispatches to, or `null` when unsupported. |

**Returns**

`self`: Definition with the action applied.

### withHosts() <span id="method-withHosts"></span>

```php
public function withHosts(list<string> $hosts): self
```

Returns a copy restricted to another set of hosts.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$hosts` | `list<string>` | Hosts the route is restricted to; empty to leave it unrestricted. |

**Returns**

`self`: Definition with the hosts applied.

### withMethods() <span id="method-withMethods"></span>

```php
public function withMethods(list<string> $methods): self
```

Returns a copy accepting another set of HTTP methods.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$methods` | `list<string>` | HTTP methods the route accepts; empty to accept any. |

**Returns**

`self`: Definition with the methods applied.

### withMiddlewares() <span id="method-withMiddlewares"></span>

```php
public function withMiddlewares(list<string>|null $middlewares): self
```

Returns a copy carrying another middleware chain.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$middlewares` | `list<string>\|null` | Middleware labels applied to the route; empty when it has none, or `null` when unsupported. |

**Returns**

`self`: Definition with the middlewares applied.

### withMode() <span id="method-withMode"></span>

```php
public function withMode(string|null $mode): self
```

Returns a copy carrying another mode.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$mode` | `string\|null` | Parsing mode the rule runs in, or `null` when unsupported. |

**Returns**

`self`: Definition with the mode applied.

### withSuffix() <span id="method-withSuffix"></span>

```php
public function withSuffix(string|null $suffix): self
```

Returns a copy carrying another suffix.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$suffix` | `string\|null` | URL suffix the rule appends, or `null` when unsupported. |

**Returns**

`self`: Definition with the suffix applied.

### withTarget() <span id="method-withTarget"></span>

```php
public function withTarget(string|null $target): self
```

Returns a copy carrying another target.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$target` | `string\|null` | Route the rule resolves to, or `null` when unsupported. |

**Returns**

`self`: Definition with the target applied.

### withType() <span id="method-withType"></span>

```php
public function withType(string|null $type): self
```

Returns a copy carrying another type.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$type` | `string\|null` | Rule class or category reported by the adapter, or `null` when unsupported. |

**Returns**

`self`: Definition with the type applied.