Limiting routing parameters for numbers only in Laravel

We may use Regular Expression [0-9]+ to accept and parse out routing URL for numbers only.

Example

<?php

/*
 |--------------------------------------------------------------------------
 | Application Routes
 |--------------------------------------------------------------------------
 |
 | Here is where you can register all of the routes for an application.
 | It's a breeze. Simply tell Laravel the URIs it should respond to
 | and give it the controller to call when that URI is requested.
 |
 */

// will return 'Hello project root' on browser request /laravel/public
Route::get('/', function()
{
	return "Hello project root";
});

Route::get('person/{id}', function($id)
{
	return "Person : " . $id; 
})
->where('id', '[0-9]+'); // using regular expression

Output

URL : /laravel/public/person/22
Output : Person : 22

Share

You may also like...