Laravel blade模板生成静态化file_put_contents方法

// 获取渲染后的视图内容
$view = view('index', compact('data'))->__toString();
// 使用PHP内置方法 file_put_contents()生成静态页面
file_put_contents("index.html", $view);

生成的文件路径为 public/index.html

详细

$view = view('index')->with('data','xxx');
$html = response($view)->getContent();
if (file_exists(storage_path() .'/demo/1.html')) {
        return response()->file(storage_path() ."/demo/1.html");
}
if (! file_exists(storage_path() .'/demo')) {
        mkdir(storage_path() .'/demo');
}
file_put_contents(storage_path() ."/demo/".'1.html',$html);
chmod(storage_path() ."/demo/'1.html", '0777');
return response()->file(storage_path() ."/demo/1.html");

 

原文地址:https://www.cnblogs.com/pxuan/p/12918225.html