更改错误视图的路径

问题描述 投票:2回答:3

从文档中我看到我只需要在resources/views/errors/中创建新的刀片文件并使用abort(newhtmlerrorno)调用它们

如果我想使用其他目录中的刀片文件,这是可能的还是必须在基本应用程序resources/views/errors/

laravel laravel-5 laravel-5.2
3个回答
3
投票

在你的App\Exceptions\Handler类中,将它添加到render方法的开头:

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

    // ...
}

视图路径上的error文件夹在Illuminate\Foundation\Exceptions\Handler类中进行了硬编码,您的本地应用程序Handler扩展了该类。因此,您不能仅在不覆盖该特定类的情况下配置自定义路径。


0
投票

找到文件App \ Exceptions \ Handler并用下面的函数render替换

public function render($request, Exception $exception)
{
    if ($this->isHttpException($exception)) {
        $statusCode = $exception->getStatusCode();
        return response()->make(view("NEW_PATH.{$statusCode}",['exception'=>$exception]), $statusCode) ;
    }
    return parent::render($request, $exception);
}

0
投票

Laravel 5.8中有一个方法\ Illuminate \ Foundation \ Exceptions \ Handler :: registerErrorViewPaths。您可以在\ App \ Exceptions \ Handler类中简单地覆盖它:

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());
}
© www.soinside.com 2019 - 2024. All rights reserved.