如何在 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, $回撥函式或控制器);

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

示例 1

使用 where() 方法

where() 方法定義在路由上,它將接收引數名稱和應用於它的驗證規則。如果有多個引數,它將接收一個數組,其中鍵為引數名稱,值為應用於鍵的驗證規則。

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

輸出

輸出為:

disha

在上述情況下,studentname 必須是 A-Z 或 a-z 或兩者的混合。因此,以下是有效的 URL:

https://:8000/student/DISHA
https://:8000/student/dishaSingh.

無效 URL:

https://: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

上述有效的 URL 為:

https://:8000/student/12/disha
https://:8000/student/01/disha

無效 URL:

https://:8000/student/01/DISHA
https://: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()

示例

您需要傳遞您希望包含字母值的路由引數:

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

更新於: 2022-08-30

11K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.