Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.7:我可以在哪里注册允许的GET参数并阻止其他参数?_Laravel_Laravel 5.7 - Fatal编程技术网

Laravel 5.7:我可以在哪里注册允许的GET参数并阻止其他参数?

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

我有简单的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&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 () {