Skip to content

Conversation

@cvairlis
Copy link

Overview

This PR adds a new FromRouteDefault attribute that allows injecting route default values into data properties.

Problem

Laravel allows setting default route values via ->defaults('key', 'value'), commonly used for route grouping and middleware logic. The existing FromRouteParameter only works with URL parameters like {parameter}, not defaults.

Solution

The FromRouteDefault attribute:

  • Injects values from route defaults set via ->defaults()
  • Follows single responsibility principle (only handles defaults)
  • Can be stacked with FromRouteParameter for parameter → default fallback behavior

Example Usage

Basic usage with route defaults:

Route::get('/songs/archived', [SongController::class, 'index'])
    ->defaults('status', 'archived');

class SongData extends Data {
    #[FromRouteDefault('status')]
    public string $status;
}

Stacking attributes for fallback behavior:

Route::get('/songs/{status}', [SongController::class, 'index'])
    ->defaults('status', 'published');

class SongData extends Data {
    #[FromRouteParameter('status')]
    #[FromRouteDefault('status')]
    public string $status;
}

When accessing /songs/archived, $status will be 'archived' (from parameter). For routes without the {status} parameter, it falls back to 'published' (from default).

What's Included

  • FromRouteDefault attribute implementation in FromRouteDefault.php
  • Comprehensive test coverage in FromRouteDefaultTest.php
  • Documentation with examples in injecting-property-values.md
  • Follows package conventions and coding style
  • Single responsibility principle maintained

Use Case

This is particularly useful for section-based routing where you have route groups with default values that need to be accessed in your data objects:

Route::prefix('admin')->defaults('area', 'admin')->group(function () {
    Route::get('/users', [UserController::class, 'index']);
});

class UserData extends Data {
    #[FromRouteDefault('area')]
    public string $area;
}

- Add FromRouteDefault attribute to inject route defaults into data properties
- Add comprehensive tests for the attribute
- Update documentation with usage examples
- Follows single responsibility principle (only handles route defaults)
- Can be stacked with FromRouteParameter for fallback behavior
@cvairlis cvairlis closed this Oct 30, 2025
@cvairlis cvairlis deleted the feature/from-route-default-attribute branch October 30, 2025 19:26
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