Skip to content

[13.x] Add route metadata support#60530

Open
benbjurstrom wants to merge 4 commits into
13.xfrom
feat/route-metadata
Open

[13.x] Add route metadata support#60530
benbjurstrom wants to merge 4 commits into
13.xfrom
feat/route-metadata

Conversation

@benbjurstrom

Copy link
Copy Markdown
Member

This PR adds support for attaching arbitrary metadata to routes.

Currently applications can pass custom keys through the route action array and read them back from the resolved route, but there's no supported way to attach structured data across the full route-building pipeline. This PR makes metadata a first-class attribute so it can be applied consistently before the final route instances are created.

Metadata can be added directly to a route:

Route::get('/users', [UserController::class, 'index'])
    ->metadata(['head' => ['title' => 'Users']]);

Metadata can be read from the route with optional dot notation and a default value:

$request->route()->getMetadata('head.title'); // 'Users'
$request->route()->getMetadata('head.author', 'Taylor');

Metadata set on a group cascades to every route inside it. Associative arrays merge recursively so nested groups can layer values, while lists and simple values replace inherited values:

Route::metadata(['head' => ['robots' => ['noindex'], 'author' => 'Taylor']])
    ->group(function () {
        Route::get('/users', [UserController::class, 'index'])
            ->metadata(['head' => ['title' => 'Users']]);
    });

// getMetadata('head') => [
//     'robots' => ['noindex'],
//     'author' => 'Taylor',
//     'title' => 'Users',
// ]

Resources and singletons are supported as well:

Route::resource('users', UserController::class)
    ->metadata(['head' => ['title' => 'Users']]);

Metadata is stored on the route action under a dedicated metadata key, allowing it to be serialized by route:cache and merged through the existing route group logic. setMetadata is also available on the route instance to replace metadata instead of merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant