Laravel 5.6-如何实施404页面/路线
我试图实现404页,但到目前为止什么都没有发生。我明白了:Laravel 5.6-如何实施404页面/路线,laravel,laravel-5.6,Laravel,Laravel 5.6,我试图实现404页,但到目前为止什么都没有发生。我明白了: Not Found The requested URL /test was not found on this server. 我有一个完全不同文本的自定义404页面 在我的路由文件中,我有以下路由: Route::fallback(function(){ return response()->view('errors/404', [], 404); }); 在Handler.php中,我添加了以下内容: /**
Not Found
The requested URL /test was not found on this server.
我有一个完全不同文本的自定义404页面
在我的路由文件中,我有以下路由:
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位于资源/view/errors下404.blade.php文件应位于资源/view/errors下(注意视图中的“s”)。您不需要在routes和Handler.php文件中使用自定义代码,Laravel可以自己处理404。错误消息
Not Found
The requested URL /test was not found on this server.
是默认的服务器404错误消息,而不是来自Laravel
当您不配置自定义错误页面时,应该会看到Laravel的默认错误页面。
这意味着您可能没有在服务器上正确配置“重写”,并且Laravel没有收到请求
您可以在handler.php immport中的上查看这篇文章
use Illuminate\Session\TokenMismatchException;
并将render()函数编辑为
public function render($request, Exception $exception)
{
if ($exception instanceof TokenMismatchException) {
if ($request->expectsJson()) {
return response()->json([
'dismiss' => __('Session expired due to inactivity. Please reload page'),
]);
}
else{
return redirect()->back()->with(['dismiss'=>__('Session expired due to inactivity. Please try again')]);
}
}
elseif($exception->getStatusCode()!=422){
return response()->view('errors.404');
}
return parent::render($request, $exception);
}
这是你将如何重定向到404页上的任何错误。
令牌不匹配异常为会话到期,状态代码422为验证错误。此处$request->expectsJson()用于ajax json进程视图中文件夹的分隔符是点而不是斜杠。尝试
returnresponse()->view('errors.404',[],404)也许是这个答案?您也可以只使用abort helper,它默认为您使用的视图。这看起来像是默认的Server404错误页面,即请求实际上没有命中Laravel。因此,您可能没有正确配置URL重写。