Skip to content

[Proposal] Set Route segment patterns globally #176

Closed
@JoostK

Description

@JoostK

With the recent addition of Router binding (a great feature!) we can now automatically bind models for a certain parameter name.

In my routes however, I always use secondary keys to retrieve models, because then the URL makes sense to users. Many times this key consists of multiple columns, so I need multiple segments to specify them.

It would be nice to also be able to specify a pattern for a parameter name globally on the Router instance. Then we specify the pattern of the parameter once and be good to go for all of them.

Route::pattern('user', '[^/]/[^/]');

Route::get('users/{user}', ['as' => 'user', function(User $user){  }]);
Route::get('users/{user}/edit', ['as' => 'user.edit', function(User $user){  }]);

For both routes the parameter pattern has now been set. This is the most general form. Even nicer would be to just be able to alias a variable:

Route::segments('user', ['firstname', 'lastname']);
// {user} in routes will now be handled as {firstname}/{lastname}, but it is only
// passed as a single input parameter (as an array) to the route (and binding hook)

This sort of 'combines' multiple segments into one parameter, and also allows for generating a url like this:

URL::route('user', ['firstname' => $user->firstname, 'lastname' => $user->lastname]);

This basically aliases multiple segments into one parameter. It may be a bit tricky to implement but it adds a lot of flexibility to the new route binding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions