Skip to content

Conversation

@gregorip02
Copy link
Contributor

This PR allows the developer to set restrictions on the route registration in a simple way. Also, drop unnecessary methods of #34361

Instead of having a statement similar to the following.

Route::get('authors/{author}/{book}')->where(['author' => '[0-9]+', 'book' => '[a-zA-Z]+']);

This same statement could be simplified with the following.

Route::get('authors/{author}/{book}')->whereNumber('author')->whereString('book');

This also supports declaration of multiple parameters of the same grouped type.

Route::get('authors/{author}/{book}')->whereString('author', 'book');

class PendingResourceRegistration
{
use Macroable;
use Macroable, RouteRegexConstraintTrait;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// This works
Route::apiResource('foo', 'FooController')->whereString('foo');

@taylorotwell
Copy link
Member

I find a whereString a bit confusing based on its name. For example, if I pass foo-bar to is_string I will get true. It is a string, it just contains dashes. Feels like your whereString is more like whereAlpha or something.

@gregorip02
Copy link
Contributor Author

@taylorotwell Makes sense, I am renaming the method...

Co-authored-by: Marek Szymczuk <marek@osiemsiedem.com>
@imanghafoori1
Copy link
Contributor

imanghafoori1 commented Oct 27, 2020

Why not use the already existing validation rules syntax?

Route::get('authors/{author}/{book}')->where('author', 'numeric');
Route::get('authors/{author}/{book}')->where('author', 'in:hi,bye');

it created a dependency between components but you find a good way to deal with it.
This way people can define their rules.
since usually in utf-8 characters (non-latin) exist in urls in other countries for SEO purposes.

@gregorip02
Copy link
Contributor Author

@imanghafoori1 Laravel has a way to validate attributes of the http request in a cleaner way. This PR tries to shorten the definition of restrictions in the definition of routes and in their respective handlers.

@rcerljenko
Copy link

@gregorip02 Can you also add whereUuid() for UUID route params ?

@gregorip02
Copy link
Contributor Author

@gregorip02 Can you also add whereUuid() for UUID route params ?

Let's see what @taylorotwell thinks, if this feature is useful I will be happy to add it. I think of an expression like the following.

# Regex pattern
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
Route::get('users/{uuid}')->whereUuid('uuid');

@martinbean
Copy link
Contributor

These are amazing. Thank you, @gregorip02!

It would be good to see that whereUuid method added. I use UUIDs a lot in my own Laravel projects.

@xorock
Copy link

xorock commented Nov 19, 2020

Great job but one question. I do not know about the others, but for me 99% of numerical restrictions in url is a pattern '[1-9][0-9]*' - allow numbers > 0. Do you also follow a similar pattern?

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.

7 participants