我创建了自定义请求类,其中我禁止访问特定的路由。假设我试图禁止通过这个请求类删除一些自定义类别。一切正常,我被重定向到403页。问题是,我如何定制重定向路径?
我浏览了互联网,看到了提到forbiddenResponse()方法,但我不能设法让它工作。
我使用的是Laravel 5.7
发布于 2018-12-01 09:24:34
如果您正在使用命名路由,如
Route::get("/user", "UserController@index")->name('user');然后你就可以这样做了。
return redirect()->route('user');发布于 2018-12-01 20:20:24
我找到了一些关于这方面的信息。从Laravel 5.4开始,forbiddenResponse函数已被删除。
通过更改位于App\Exceptions文件夹中的Handler.php文件,我成功地完成了所需的操作。在render函数中,我检查Exception是否为AuthorizationException,然后重定向到另一个页面。
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Auth\Access\AuthorizationException) {
return response()->redirectTo(route('categories.index'));
}
return parent::render($request, $exception);
}https://stackoverflow.com/questions/53565259
复制相似问题