Laravel 5.7:我可以在哪里注册允许的GET参数并阻止其他参数?
我有简单的Laravel应用程序,我有自定义的允许获取我的应用程序参数:Laravel 5.7:我可以在哪里注册允许的GET参数并阻止其他参数?,laravel,laravel-5.7,Laravel,Laravel 5.7,我有简单的Laravel应用程序,我有自定义的允许获取我的应用程序参数: $allowedGetParameters = [ 'user', 'event', 'action' ] 如何阻止数组中除指定参数之外的所有其他GET参数 例如,可能的URL地址: - https://app.com/?user=16 - https://app.com/?event=242&user=16 - https://app.com/?user=16&event=242&am
$allowedGetParameters = [
'user',
'event',
'action'
]
如何阻止数组中除指定参数之外的所有其他GET参数
例如,可能的URL地址:
- https://app.com/?user=16
- https://app.com/?event=242&user=16
- https://app.com/?user=16&event=242&action=like
- https://app.com/?user=16&post=43&like=true
- https://app.com/?guru=242&set=superguru&action=true
带有其他GET参数的URL必须返回404响应。此处示例URL:
- https://app.com/?user=16
- https://app.com/?event=242&user=16
- https://app.com/?user=16&event=242&action=like
- https://app.com/?user=16&post=43&like=true
- https://app.com/?guru=242&set=superguru&action=true
注意:
在这种情况下,如果URL包含一个或多个不允许的GET参数,包括或不包括允许的GET参数,则结果也应返回404。创建中间件
执行handle
方法,在这里进行检查:
$params = array_keys($request->all());
$is_valid_params = count(array_diff($params, $allowedGetParameters)) == 0;
$is_get_request = $request->method() == 'GET';
if ($is_valid_params && $is_get_request) {
return $next($request);
};
return abort(404);
另外,我会将$allowedGetParameters移动到somefile.php中的config文件夹中,并会这样访问:count(array_diff($params,config('somefile.allowedGetParameters'))==0;
别忘了:
1) 在protected$routeMiddleware
app\Http\Kernel.php中注册您的中间件
2) 在web.php中使用以下内容包装您的路线:
Route::group(['middleware' => ['name_of_your_widdleware']], function () {