Route patterns

Submitted by stidges - 11 years ago

Sometimes you would like to restrict a certain route parameter. For instance, if you are using route parameters to determine the ID of a certain model you are using, you would only like digits to be passed, not alphabetical characters. You can set these restrictions on the route using ->where('<pattern>'). But what if you have a huge list of routes that all use the same pattern restriction? That's where this will come in handy.

// You would usually do it this way..
Route::get('post/{post_id}', 'PostsController@show')->where('\d+');
Route::get('post/{post_id}/edit', 'PostsController@edit')->where('\d+');
Route::put('post/{post_id}', 'PostsController@update')->where('\d+');
// etc...

// You can simplify this greatly..
Route::pattern('post_id', '\d+');

// Now you don't have to specify the restrictions every single time!
Route::get('post/{post_id}', 'PostsController@show');
Route::get('post/{post_id}/edit', 'PostsController@edit');
Route::put('post/{post_id}', 'PostsController@update');