从文档中我看到我只需要在resources/views/errors/
中创建新的刀片文件并使用abort(newhtmlerrorno)
调用它们
如果我想使用其他目录中的刀片文件,这是可能的还是必须在基本应用程序resources/views/errors/
中
在你的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
扩展了该类。因此,您不能仅在不覆盖该特定类的情况下配置自定义路径。
找到文件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);
}
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());
}