Reputation: 8705
I am trying to implement 404 page, but so far nothing is happening. I am getting this:
Not Found
The requested URL /test was not found on this server.
I have custom 404 page with completely different text.
In my routes file I have this route:
Route::fallback(function(){
return response()->view('errors/404', [], 404);
});
In Handler.php I added this:
/**
* 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);
}
The 404.blade.php is located under the resources/view/errors
Upvotes: 1
Views: 2278
Reputation: 19
in handler.php immport
use Illuminate\Session\TokenMismatchException;
and edit the render() function to following
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);
}
This is how you will be redirected to 404 page on any error. TokenMismatchException is session expiration and status code 422 is validation error. Here $request->expectsJson() is for ajax json process
Upvotes: 0
Reputation: 2893
The error message
Not Found
The requested URL /test was not found on this server.
is the default server 404 error message and not from Laravel.
You are supposed to see Laravel's default error page when you don't configure custom error pages. This means that you might not have configured rewrite properly on your server and Laravel did not get the request.
You can check out this post on how to enable mod_rewrite on apache
Upvotes: 0
Reputation: 13579
The 404.blade.php file should be located under resources/views/errors (note the 's' in views). And you don't need that custom code in your routes and Handler.php files, Laravel can handle 404's by itself.
Upvotes: 1