在 Laravel 中,路由在 paths/ 文件夾中定義。路由在 web.php 文件中定義。該文件是在 laravel 安裝完成后創(chuàng)建的。 Laravel 路由接受 URI 和閉包函數(shù),如下所示 -
use Illuminate\Support\Facades\Route; Route::get('/student', function () { return 'Hello Student'; });
在web/routes.php中定義的路由被分配到web中間件組中,并且它們 具有會(huì)話狀態(tài)和CSRF保護(hù)。您還可以在路由中調(diào)用控制器 如下所示 -
use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']);
以下是您可以在應(yīng)用程序中使用的路由方法:
Route::get($ uri, $回調(diào)函數(shù)或控制器);
Route::post($uri, $回調(diào)函數(shù)或控制器);
Route::put($uri, $回調(diào)函數(shù)或控制器);
Route::patch($uri, $回調(diào)函數(shù)或控制器);
Route::delete($uri, $回調(diào)函數(shù)或控制器);
Route::options($uri, $回調(diào)函數(shù)或控制器);
路線參數(shù)位于大括號(hào)內(nèi),并且給出的名稱包含字母數(shù)字字符。除了字母數(shù)字之外,您在選擇路由參數(shù)名稱時(shí)還可以使用下劃線。
路由參數(shù)的語(yǔ)法如下所示?
Route::get('/user/{myid}', function ($myid) { // });
這里myid是我們要進(jìn)一步使用的路由參數(shù)。
您可以像下面的語(yǔ)法所示,擁有多個(gè)路由參數(shù)。
Route::get('/students/{post}/feedbacks/{feedback}', function ($postId, $feedbackId) { // });
在上述情況下,有兩個(gè)路由參數(shù):{post}和{feedback}
您還可以為路由添加可選參數(shù)??蛇x參數(shù)并不總是可用,參數(shù)后面用?表示。可選參數(shù)的語(yǔ)法如下所示 ?
Route::get('/students/{myname?}', function ($myname = null) { return $myname; });
這里 myname 是一個(gè)可選參數(shù)。
Laravel有一些方法可以幫助驗(yàn)證參數(shù)。它們是where(),whereNumber(),whereAlpha()和whereAlphaNumeric()。
使用where()方法
where()方法在路由上定義,它將接受參數(shù)名稱和應(yīng)用于參數(shù)的驗(yàn)證。如果有多個(gè)參數(shù),它將以數(shù)組形式接受,其中鍵為參數(shù)名稱,值為要應(yīng)用于鍵的驗(yàn)證規(guī)則。
Route::get('/student/{studentname}', function ($studentname) { return $studentname; })->where('studentname', '[A-Za-z]+');
輸出為 ?
disha
在上述情況下,學(xué)生姓名必須包含 A-Z 或 a-z 或兩者的混合。因此以下是有效的網(wǎng)址 -
http://localhost:8000/student/DISHA http://localhost:8000/student/dishaSingh.
無(wú)效網(wǎng)址 -
http://localhost:8000/student/dishaSingh123
現(xiàn)在讓我們使用 where() 方法檢查多個(gè)參數(shù)。
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname){ return $studentid."===".$studentname; })->where(['studentid' => '[0-9]+', 'studentname' => '[a-z]+']);
上述的輸出為?
12===disha
上述的有效網(wǎng)址為?
http://localhost:8000/student/12/disha http://localhost:8000/student/01/disha
無(wú)效網(wǎng)址 -
http://localhost:8000/student/01/DISHA http://localhost:8000/student/abcd/disha
您需要傳遞您希望僅為有效值的路由參數(shù) -
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->where('studentname','[a-z]+');
上述代碼的輸出為 ?
12===disha
您需要傳遞您希望具有 alpha 值的路由參數(shù) -
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlpha('studentname');
上述代碼的輸出為 ?
12===dishaSingh
您需要傳遞您希望具有字母數(shù)字值的路由參數(shù)?
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->whereNumber('studentid')->whereAlphaNumeric ('studentname');
輸出將是 -
12===dishaSingh122
以上就是如何在Laravel中驗(yàn)證路由參數(shù)?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
路由優(yōu)化大師是一款及簡(jiǎn)單的路由器設(shè)置管理軟件,其主要功能是一鍵設(shè)置優(yōu)化路由、屏廣告、防蹭網(wǎng)、路由器全面檢測(cè)及高級(jí)設(shè)置等,有需要的小伙伴快來(lái)保存下載體驗(yàn)吧!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)