我正在尝试实现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中,我添加了以下内容:
/**
* 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下面
发布于 2018-07-10 14:24:49
404.blade.php文件应该位于(注意视图中的's‘)下。而且您不需要在您的路线和Handler.php文件中的自定义代码,Laravel可以自己处理404。
发布于 2018-07-10 15:14:28
错误信息
Not Found
The requested URL /test was not found on this server.是默认服务器404错误消息,而不是来自Laravel。
当您不配置自定义错误页时,您应该看到Laravel的默认错误页。这意味着您可能没有在服务器上正确地配置重写,而Laravel没有收到请求。
你可以在rewrite on apache上查看这篇文章
发布于 2018-07-10 15:34:40
在handler.php免疫中
use Illuminate\Session\TokenMismatchException;并将呈现()函数编辑为
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页。TokenMismatchException是会话过期,状态代码422是验证错误。这里的$request->expectsJson()是用于ajax进程的
https://stackoverflow.com/questions/51265742
复制相似问题