Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Last active May 15, 2018 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarektkaczyk/1e3d5bc26390d4a6602b6a9b49b5b5e3 to your computer and use it in GitHub Desktop.
Save jarektkaczyk/1e3d5bc26390d4a6602b6a9b49b5b5e3 to your computer and use it in GitHub Desktop.
regex in route parameters
<?php
// Do this (explicit routes are better):
Route::get('qualification', function () {/**/});
Route::get('qualification/{year}/{month}', function ($year, $month) {/**/});
// You could also do this (but don't in your case):
// @link https://laravel.com/docs/5.6/routing#parameters-regular-expression-constraints
Route::get('qualification/{year_month?}', function ($year_month = null) {
if ($year_month) {
[$year, $month] = explode('/', $year_month);
return 'year: ' . $year . ', month: ' . $month;
}
return 'no year_month param';
// This will match '2018/05' but not '2018/5'
})->where('year_month', '\d{4}/\d{2}');
// This will match both:
})->where('year_month', '\d{4}/\d{1,2}');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment