How to validate Route Parameters in Laravel?


In laravel the routes are defined inside routes/ folder.The routes are defined inside web.php file. The file is created when the laravel installation is done. Laravel routes take in a URI and a closure function as shown below −

use Illuminate\Support\Facades\Route; Route::get('/student', function () { return 'Hello Student'; });

The routes defined inside web/routes.php are assigned a web middleware group and they have session states and CSRF protection. You can also call your controller inside routes as shown below −

use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']);

Following are the route methods you can make use in your application −

  • Route::get($ uri, $callbackfunction or controller);

  • Route::post($uri, $callbackfunction or controller);

  • Route::put($uri, $callbackfunction or controller);

  • Route::patch($uri, $callbackfunction or controller);

  • Route::delete($uri, $callbackfunction or controller);

  • Route::options($uri, $callbackfunction or controller);

Validation of Route Parameters

The route parameters are available inside the curly braces and the name given has alphanumeric characters. Along with alphanumeric you can also make use of underscore when choosing the name for your route params.

Syntax

The syntax for route parameters is as shown below −

Route::get('/user/{myid}', function ($myid) {
   //
});

Here myid is the route parameter that we want to use further.

Multiple Route Params

You can have more than one route parameter as shown in the syntax below −

Route::get('/students/{post}/feedbacks/{feedback}', function ($postId, $feedbackId) {
   //
});

In above case there are two route params: {post} and {feedback}

Optional Params

You can also have optional params to your route. Optional params will not be available all the time and the same is denoted with ? after the parameter. The syntax for optional param is as shown below −

Route::get('/students/{myname?}', function ($myname = null) {
   return $myname;
});

Here myname is an optional parameter.

Laravel has few methods that help in validation of the params. They are where(), whereNumber(), whereAlpha() and whereAlphaNumeric().

Example 1

Using where() method

The where() method is defined on the route and it will take the param name and the validation that is applied on it. If there are multiple params it will take in array with key as param name and value as the validation rule to be applied on the key.

Route::get('/student/{studentname}', function ($studentname) { return $studentname; })->where('studentname', '[A-Za-z]+');

Output

The output is −

disha

In the above case the studentname has to be either with A-Z or a-z or a mixture of both. So following are valid urls −

http://localhost:8000/student/DISHA
http://localhost:8000/student/dishaSingh.

Invalid urls −

http://localhost:8000/student/dishaSingh123

Example 2

Let us now check multiple parameters with where() method.

Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname){ return $studentid."===".$studentname; })->where(['studentid' => '[0-9]+', 'studentname' => '[a-z]+']);

In the above case the route params are studentid and studentname. The studentid has to be a number from 0-9 and studentname has to be in lower case.

Output

The output for above is −

12===disha

The valid urls for above are −

http://localhost:8000/student/12/disha
http://localhost:8000/student/01/disha

Invalid urls −

http://localhost:8000/student/01/DISHA
http://localhost:8000/student/abcd/disha

Using whereNumber()

Example

You need to pass the route param that you wish to have only numbers to be valid values −

Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->where('studentname','[a-z]+');

Output

The output of the above code is −

12===disha

Using whereAlpha()

Example

You need to pass the route param that you wish to have alpha values −

Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlpha('studentname');

Output

The output of the above code is −

12===dishaSingh

Using whereAlphaNumeric()

Example

You need to pass the route param that you wish to have alpha numeric values −

Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlphaNumeric ('studentname');

Output

Output will be −

12===dishaSingh122

Updated on: 30-Aug-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements