Skip to content

Instantly share code, notes, and snippets.

@obaid
Forked from Braunson/routing_patterns.php
Created October 23, 2017 21:08
Show Gist options
  • Save obaid/69d47ec21b683a9e697c6903aad62696 to your computer and use it in GitHub Desktop.
Save obaid/69d47ec21b683a9e697c6903aad62696 to your computer and use it in GitHub Desktop.
Routing patterns from laravel-tricks.com
<?php
// This is what you might have right now
Route::get('users/{id}', 'UserController@getProfile')->where('id', '[\d+]+');
Route::get('products/{id}', 'ProductController@getProfile')->where('id', '[\d+]+');
Route::get('articles/{slug}', 'ArticleController@getFull')->where('slug', '[a-z0-9-]+');
Route::get('faq/{slug}', 'FaqController@getQuestion')->where('slug', '[a-z0-9-]+');
// and many more, now imagine you'll have to change the rule
// Instead, you could have a handy list of patterns and reuse them everywhere:
// Patterns
Route::pattern('id', '\d+');
Route::pattern('hash', '[a-z0-9]+');
Route::pattern('hex', '[a-f0-9]+');
Route::pattern('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');
Route::pattern('base', '[a-zA-Z0-9]+');
Route::pattern('slug', '[a-z0-9-]+');
Route::pattern('username', '[a-z0-9_-]{3,16}');
// make more of your own to suit your needs: email, password, etc.
Route::get('users/{id}', 'UserController@getProfile');
Route::get('products/{id}', 'ProductController@getProfile');
Route::get('articles/{slug}', 'ArticleController@getFull');
Route::get('faq/{slug}', 'FaqController@getQuestion');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment