如何显式运行刀片编译器?

问题描述

例如,在“ resources / views / home.blade”处有一个刀片文件,因此我们想将该文件编译为“ root / public”位置上的纯PHP文件,是否有任何artisan命令或其他任何要做任务吗?

解决方法

您可以直接使用BladeCompiler

<?php

/** @var \Illuminate\View\Compilers\BladeCompiler */
$compiler = app('blade.compiler');
$srcPath = resource_path('views/home.blade.php');
$targetPath = public_path('home.php');

$compiled = $compiler->compileString(file_get_contents($srcPath));
file_put_contents($targetPath,$compiled);