如何在Laravel中驗證路由參數?

PHPz
發布: 2023-09-01 14:42:02
轉載
786 人瀏覽過

如何在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.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!