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

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

How to block IPv6 IP addresses in .htaccess

What is camel case?

How to see all of the options for an artisan command?

How to redirect your website to a new domain/location in .htaccess

Why should you update APP_URL from from http://localhost?

How to add a name prefix to all routes in a group?

What is the loop variable in a @foreach loop in Blade?

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How to make multiple routes share the same URL structure or URL parameters