What are route patterns in Laravel?

Laravel: What are route patterns in Laravel?

In your routes file you can add code like this:

  1.     Route::pattern('id', '[0-9]+');

Then you can use that pattern later on in your routes file like this:

  1. Route::get('user/{id}', function ($id) {
  2.     // Only executed if {id} is numeric...
  3. });

I've not seen this used too often, however this is a useful feature and keeps things quite clean.

Some other examples of defining some route patterns:

  1. Route::pattern('slug', '[a-z0-9-]+');
  2. Route::pattern('hash', '[a-z0-9]+');
  3.  
  4. Route::get('forum-thread/{slug}', '[email protected]'); // {slug} has to be alpha numeric (lowercase), but can include a dash
  5. Route::get('forum-post/{hash}', '[email protected]'); // {hash} has to be alpha numeric, lowercase

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about What are route patterns in Laravel?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

What is the difference between empty(), isset(), != null, is_null() in PHP

What are Template literals (Template strings) in JS?

Push vs Pop vs Shift vs Unshift

How to manually write your own pagination in PHP

How to return JSON data to the user in Laravel?

How to provide a default model object for relationships?

How to add and remove items to an array in JS?

How to destroy or remove a cookie in PHP

How to check if a user is logged in, inside a blade file?

How to enable the rewrite engine in .htaccess?