Laravel 是否可以加载扩展名为 .html 的视图模板?
我正在重建一个现有的应用程序,其中包含一堆由用户上传的 .html 文件。这是一种多租户应用程序,每个用户都可以通过上传模板来控制其区域的外观和感觉。
我需要重建应用程序并使更改对用户完全透明,因此我想保留 .html 扩展名。
我发现的最好方法是在基本控制器中使用 View::addExtension;
这是我的代码示例:
View::addExtension('blade.html','blade');
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
// Allows us to use easy-to-edit html extension files.
// You can set 2nd param to 'php' is you want to
// just process with php (no blade tags)
View::addExtension('blade.html','blade');
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
2023 年更新 - Laravel 10
Laravel 10 知道文件夹中的文件类型
resources/views
:
protected $extensions = [
'blade.php' => 'blade',
'php' => 'php',
'css' => 'file',
'html' => 'file',
];
正如您在
Illuminate\View\Factory
中看到的。
恐怕 Blade 引擎仅加载
.php
和 .blade.php
文件。
根据您的描述,我假设视图文件是静态的,因为它们只是 HTML。 如果是这样,请在用户上传视图文件后将它们重命名为
.php
- 不应给您的应用程序带来性能影响,因为无论如何都没有什么需要处理的。