1

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

Sasha
  • 7,725
  • 21
  • 78
  • 154
  • The separator for folders within views is a dot not a slash. try `return response()->view('errors.404', [], 404);` – Joe Jul 10 '18 at 12:57
  • Maybe this answer? https://stackoverflow.com/a/51256791/3585500 – ourmandave Jul 10 '18 at 12:59
  • You could also just use the abort helper https://laravel.com/docs/5.6/helpers#method-abort which defaults to the view you have used. – Joe Jul 10 '18 at 13:00
  • That looks like the default server 404 error page, i.e. the request isn’t actually hitting Laravel. So you’ve probably not configured URL-rewriting properly. – Martin Bean Jul 10 '18 at 15:21

3 Answers3

1

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.

Agu Dondo
  • 9,636
  • 6
  • 50
  • 59
0

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

Victor Anuebunwa
  • 1,493
  • 20
  • 29
0

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