Laravel Route Parameter Validation: 'Where' in Routes File

preview_player
Показать описание
I want to show an old "trick" that I remembered recently, and maybe some of you don't know it. How to validate route parameters directly in the routes file?

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Great to know! Laravel (and you) never stop to surprise me! thanks a lot

AntoninoDiGiglio
Автор

It's worth noting that the expression you pass to 'where' is a Regular Expression and so can be as complex as you like... but there's a word of caution in that in that complex regular expressions tend to hinder readability, can become hard to maintain, and can introduce hard to spot bugs. So in cases where the validation is more complex I tend to prefer to write that code in plain old PHP.

martinh
Автор

This reminded of explicit model binding. One of my coworker asking me why i had to use it instead of the implicit one. Idk if it just overlooked or what

ilhamfajriumar
Автор

is it possible to validate {route parameters} inside FormRequest rules.?
Eg:

and inside the rules,
"user_id" => 'required|exists:users, id

AthulrajDR
Автор

I like to validate my path parameter in a form request. Just pass the data to the validationdata method in there and validate it like it is a simple query parameter.

JohnnyBigodes
Автор

What is better, Auth::user or auth()->user() ?

snikkerrr
Автор

Is it possible to validate get/route params like Form request in separate request file?

pawelxyztemp
Автор

Not sure, since touched Laravel for quite some time ago, but isn't optional parameters are with leading question mark, not a trailing one?
{?param} vs {param?}

I can forget about such small detaild and will have to google them once in a while -___-

cardboarddignity
Автор

technically it's not validation but tightening the route definition

ZKIUS