我想保存我的 blade
模板到数据库,因为 header and footer
每页is customizable
对于用户。我想让我的用户自己创建布局,然后对于来自给定用户的每个请求,我想使用该用户指定的布局为页面提供服务。
Controller 传递的必要变量在文档中提供给他们。
注意:我信任我的用户。他们都是项目的利益相关者,都是程序员,所以服务器端代码执行是可以接受的。
最佳答案
虽然这是一个旧帖子,但以防万一有人像我一样偶然发现它。我在使用 Laravel 框架时实现了类似的效果,通过将 View 保存在数据库中,这样,每当我需要显示 View 时,我都会从数据库中检索它,并使用 file_put_contents()
将其加载到文件中。 php 函数并使用 view()
渲染它方法。例如;
$blade = DB::table('pages')->where('name', 'index')->first();
file_put_contents('template.blade.php', $blade->view);
//Note if I also need to pass data to the view I can also pass it like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
// return view(template, $data);
return view('template');
再次在我自己的情况下,为了增加安全性,我使用 Blade 模板方案创建了基本模板,并在使用 HTMLPurifier 清理生成的输入后将用户创建的输入注入(inject)模板中。并渲染 View 。例如
$view = view('base.template')->render();
//similarly like the above I can load any data into the view like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
//$view = view('base.template', $data)->render();
$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
$with_purified_input = $purifier->purify($user_generated_input);
str_replace('view_variable', $with_purified_input, $view);
return $view;
关于php - 将 Blade 模板保存到数据库而不是文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489775/