Route Parameters Validation with RegExp

We can validate parameters directly in the route, with “where” parameter. A pretty typical case is to prefix your routes by language locale, like fr/blog and en/article/333. How do we ensure that those two first letters are not used for some other than language?

routes/web.php:

1Route::group([
2 'prefix' => '{locale}',
3 'where' => ['locale' => '[a-zA-Z]{2}']
4], function () {
5 Route::get('/', 'HomeController@index');
6 Route::get('article/{id}', 'ArticleController@show');
7});

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord