【发布时间】:2018-12-18 07:54:52
【问题描述】:
我正在尝试实现 404 页面,但到目前为止什么都没有发生。我得到了这个:
Not Found
The requested URL /test was not found on this server.
我有自定义的 404 页面,其中的文字完全不同。
在我的 routes 文件中,我有这条路线:
Route::fallback(function(){
return response()->view('errors/404', [], 404);
});
在 Handler.php 我添加了这个:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof MethodNotAllowedHttpException)
abort(404);
if ($this->isHttpException($exception)) {
if ($exception->getStatusCode() == 404) {
return response()->view('errors.404', [], 404);
}
}
return parent::render($request, $exception);
}
404.blade.php 位于 resources/view/errors
下【问题讨论】:
-
视图中文件夹的分隔符是点而不是斜线。试试
return response()->view('errors.404', [], 404); -
你也可以只使用中止助手laravel.com/docs/5.6/helpers#method-abort,它默认为你使用的视图。
-
这看起来像默认的服务器 404 错误页面,即请求实际上并没有命中 Laravel。所以你可能没有正确配置 URL 重写。
标签: laravel laravel-5.6