Search code examples
phplaravellaravel-bladeassets

how to run the blade compiler explicitly?


for example there is a blade file at "⁨resources⁩/views/home.blade" so we want to compile the file into plain php file at location "root/⁩public", is there any artisan command or anything to do the task?


Solution

  • You can use the BladeCompiler directly:

    <?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);