Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change path to error views

From the documentation I see that I simply have to create new blade files within resources/views/errors/ and call them using abort(newhtmlerrorno)

If I want to use blade files from another directory, is this possible or must they be in the base app resources/views/errors/

like image 935
myol Avatar asked Aug 20 '26 08:08

myol


2 Answers

In your App\Exceptions\Handler class, add this to the start of your render method:

public function render($request, Exception $e)
{
    if ($this->isHttpException($e)) {
        $statusCode = $e->getStatusCode();
        return view("custom.path.{$statusCode}");
    }

    // ...
}

The error folder on the view path is hardcoded in Illuminate\Foundation\Exceptions\Handler class, which your local app Handler extends. So you can't just configure a custom path without overriding that particular class.

like image 100
Tomas Buteler Avatar answered Aug 23 '26 01:08

Tomas Buteler


There is a method \Illuminate\Foundation\Exceptions\Handler::registerErrorViewPaths in Laravel 5.8. You can simply override it in your \App\Exceptions\Handler class:

protected function registerErrorViewPaths()
{
    $paths = collect(config('view.paths'));

    View::replaceNamespace('errors', $paths->map(function ($path) {
        return "{$path}/YOUR_CUSTOM_PATH/errors";
    })->push(__DIR__.'/views')->all());
}
like image 25
Alies Avatar answered Aug 23 '26 00:08

Alies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!