Last active
May 15, 2018 12:20
-
-
Save jarektkaczyk/1e3d5bc26390d4a6602b6a9b49b5b5e3 to your computer and use it in GitHub Desktop.
regex in route parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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