Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable laravel 5.4 default error handler

I am currently working on a laravel project. I need to redirect all error pages to 404 page not found page.

public function render($request, Exception $exception)
        {
            if ($this->isHttpException($exception)) {
                switch ($exception->getStatusCode()) {

                        // not authorized
                        case '403':
                                return \Response::view('404',array(),403);
                                break;

                        // not found
                        case '404':
                                return \Response::view('404',array(),404);
                                break;

                        // internal error
                        case '500':
                                return \Response::view('404',array(),500);
                                break;

                        default:
                                return $this->renderHttpException($exception);
                                break;
                }
        } else {
                return parent::render($request, $exception);
        }



                return parent::render($request, $exception);
        }

Is there anyway to redirect error page to 404 page?. Also the validation errors are not displaying in this code(Redirecting to 404 when validation errors occurs). I am using the version 5.4.

like image 951
Aneesh Ajithkumar Avatar asked Mar 06 '18 04:03

Aneesh Ajithkumar


People also ask

How do I change to 404 in laravel?

Create Custom 404 Error Page You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.

How do I enable error reporting in laravel?

Through your config/app. php , set 'debug' => env('APP_DEBUG', false), to true . Or in a better way, check out your . env file and make sure to set the debug element to true.


2 Answers

Its Bug of Laravel 5.4 modified on laravel 5.5

https://github.com/laravel/framework/pull/18481

change file vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php

if (! $this->isHttpException($e) && config('app.debug')) {
        return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
    }
    if (! $this->isHttpException($e)) {
       return \Response::view('404',array(),500);
    }
    return $this->toIlluminateResponse($this->renderHttpException($e), $e);
like image 96
Anandu Krishna Avatar answered Oct 03 '22 20:10

Anandu Krishna


How about:

if ($this->isHttpException($exception)) {
    abort(404);
}
like image 33
DevK Avatar answered Oct 03 '22 21:10

DevK