Skip to main content
Category:

Route Parameters Validation with RegExp in Laravel


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


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

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook