如何在 Laravel 中验证路由参数?
在 Laravel 中,路由定义在 routes/ 文件夹中。路由定义在 web.php 文件中。该文件在 Laravel 安装完成后创建。Laravel 路由接受一个 URI 和一个闭包函数,如下所示 -
use Illuminate\Support\Facades\Route; Route::get('/student', function () { return 'Hello Student'; });
在 web/routes.php 中定义的路由会被分配一个 Web 中间件组,并具有会话状态和 CSRF 保护。您也可以在路由内部调用控制器,如下所示 -
use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']);
以下是您可以在应用程序中使用的路由方法 -
Route::get($ uri, $callbackfunction 或 controller);
Route::post($uri, $callbackfunction 或 controller);
Route::put($uri, $callbackfunction 或 controller);
Route::patch($uri, $callbackfunction 或 controller);
Route::delete($uri, $callbackfunction 或控制器);
Route::options($uri, $callbackfunction 或 controller);
路由参数验证
路由参数在花括号内可用,名称由字母数字字符组成。除了字母数字字符外,您还可以使用下划线来为路由参数命名。
语法
路由参数的语法如下所示 -
Route::get('/user/{myid}', function ($myid) {
//
});
这里的 myid 是我们要进一步使用的路由参数。
多个路由参数
您可以拥有多个路由参数,如以下语法所示 -
Route::get('/students/{post}/feedbacks/{feedback}', function ($postId, $feedbackId) {
//
});
上述示例中有两个路由参数:{post} 和 {feedback}。
可选参数
您还可以在路由中添加可选参数。可选参数并非始终可用,在参数后用 ? 表示。可选参数的语法如下所示:-
Route::get('/students/{myname?}', function ($myname = null) {
return $myname;
});
此处 myname 是可选参数。
Laravel 提供了一些方法来帮助验证参数。它们分别是 where()、whereNumber()、whereAlpha() 和 whereAlphaNumeric()。
示例 1
使用 where() 方法
where() 方法定义在路由上,它将接收参数名称和应用于该参数的验证。如果有多个参数,它将接收一个数组,其中键作为参数名称,值作为应用于该键的验证规则。
Route::get('/student/{studentname}', function ($studentname) { return $studentname; })->where('studentname', '[A-Za-z]+');
输出
输出为 −
disha
在上述示例中,学生姓名必须是 A-Z 或 a-z 或两者混合。因此,以下是有效的 URL:−
http://localhost:8000/student/DISHA http://localhost:8000/student/dishaSingh.
无效 URL:−
http://localhost:8000/student/dishaSingh123
示例 2
现在让我们使用 where() 方法检查多个参数。
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname){ return $studentid."===".$studentname; })->where(['studentid' => '[0-9]+', 'studentname' => '[a-z]+']);
在上述示例中,路由参数为 studentid 和 studentname。studentid 必须是 0-9 之间的数字,studentname 必须是小写。
输出
上面的输出是 −
12===disha
以上有效网址为 −
http://localhost:8000/student/12/disha http://localhost:8000/student/01/disha
无效网址 −
http://localhost:8000/student/01/DISHA http://localhost:8000/student/abcd/disha
使用 whereNumber()
示例
您需要传递仅包含数字作为有效值的路由参数 −
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->where('studentname','[a-z]+');
输出
上述代码的输出为 −
12===disha
使用 whereAlpha()
示例
您需要传递希望具有 alpha 值的路由参数 −
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlpha('studentname');
输出
上述代码的输出为 −
12===dishaSingh
使用 whereAlphaNumeric()
示例
您需要传递希望使用字母数字值的路由参数 −
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlphaNumeric ('studentname');
输出
输出将是 −
12===dishaSingh122

