获取请求参数
使用 $_GET 数组可获取 GET 请求的参数
Route::get('/qmark', function(){echo "The id is " . $_GET['id'];});// GET请求:BaseURL/qmark?id=1213317 ,则 $_GET['id'] 取得值 1213317
获取路径值
使用大括号 {} 将路由路径包装成路由参数,可在回调函数中获取到该路径对应的值
- 路由参数有必选
{param}、可选{param?}两种类型 - 回调函数
function的参数,并不需要跟路由参数的名称一致。 - 可选参数的路由, 如果不传递该参数, 则
function中需要为可选值配置默认值, 否则会报参数值个数不匹配的错误 ```php // {para} 对应的路径值,会被赋值给 $id Route::get(‘/para/{para}’, function($id) { echo “The Parameter is “ . $id; });
// {para?} 表示为可选参数,需要为回调函数的对应参数配置默认值
Route::get(‘/optionalPara/{para?}’, function($id = ‘默认值’) {
echo “The Optional Parameter is “ . $id;
});
<a name="AV8n9"></a>
# 参数正则校验
> 使用 where 方法为路由设置正则表达式匹配校验,如果路径不匹配正则会报 `NotFoundHttpException` 错误
- where 方法传递多个参数时, 使用关联数组
```php
$this where(array|string $name, string $expression = null)
示例代码
// 路由的参数id必须是数字类型
Route::get('/routeTest/{id}', function($id){
}) -> where('id', '\d+');
// where方法传递多个参数时, 使用关联数组
Route::get('/routeTest/{id}', function($id){
}) -> where(['name' => '\w+', 'age' => '\d+']);
