如何在Laravel中路由过程中防止多个斜杠?http://localhost/page/1 - work and it work...http://localhost/////page/1 -应该返回404或重定向此错误也在Laravel网站上。https://laravel.com////docs/5.5/errors#http-exceptions
发布于 2020-06-26 23:21:55
需要说明的是,这不是一个bug。bug意味着这是框架开发人员明确表示不应该发生的事情,但实际发生了。这主要是一种未定义的行为。
发生这种情况的原因是因为请求确定当前路径的方式:
public function path()
{
$pattern = trim($this->getPathInfo(), '/');
return $pattern == '' ? '/' : $pattern;
}这将从当前pathinfo修剪/以获取路径。这并不是不合理的做法,因为您通常不需要前导斜杠和尾随斜杠,但这样做的结果是,具有许多前导斜杠和尾随斜杠的路径也将像https://laravel.com////docs/5.5/errors/////#http-exceptions一样工作
发生这种情况的原因是由于Route::getValidators() (特别是使用$request->path()的UriValidator )返回的标准路由验证器
如果你真的坚持要“修复”这个问题,那么你可以添加一个自定义的验证器来检查这个东西:
class MyUriValidator implements ValidatorInterface {
public function matches(Route $route, Request $request)
{
$path = $request->getPathInfo();
return preg_match($route->getCompiled()->getRegex(), rawurldecode($path));
}
}然后,您可以在AppServiceProvider中注册这个额外的验证器
public function boot() {
Route::$validators = array_merge(Route::getValidators(), [ new MyUriValidator ]);
}https://stackoverflow.com/questions/62597028
复制相似问题