如何在Laravel中验证路由参数?

PHPz
PHPz 转载
2023-09-01 14:42:02 156浏览

如何在Laravel中验证路由参数?

在 Laravel 中,路由在 paths/ 文件夹中定义。路由在 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, $回调函数或控制器);

  • Route::post($uri, $回调函数或控制器);

  • Route::put($uri, $回调函数或控制器);

  • Route::patch($uri, $回调函数或控制器);

  • Route::delete($uri, $回调函数或控制器);

  • Route::options($uri, $回调函数或控制器);

路由参数验证

路线参数位于大括号内,并且给出的名称包含字母数字字符。除了字母数字之外,您在选择路由参数名称时还可以使用下划线。

语法

路由参数的语法如下所示−

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()。

Example 1

的中文翻译为:

示例1

使用where()方法

where()方法在路由上定义,它将接受参数名称和应用于参数的验证。如果有多个参数,它将以数组形式接受,其中键为参数名称,值为要应用于键的验证规则。

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

输出

输出为 −

disha

在上述情况下,学生姓名必须包含 A-Z 或 a-z 或两者的混合。因此以下是有效的网址 -

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

无效网址 -

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 之间的数字,学生姓名必须小写。 需要翻译的内容为:必须是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

以上就是如何在Laravel中验证路由参数?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除