Created
September 13, 2018 23:49
-
-
Save dharkness/142b2275783ba6a9084bf098c7b712a9 to your computer and use it in GitHub Desktop.
Laravel command to compile all views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Blade; | |
use Symfony\Component\Finder\Finder; | |
use Throwable; | |
/** | |
* Compiles all the views so requests don't need to wait. | |
* | |
* @see https://github.com/laravel/ideas/issues/488#issuecomment-421133519 | |
*/ | |
class CompileViewsCommand extends Command | |
{ | |
/** | |
* @var string | |
*/ | |
protected $signature = 'view:compile'; | |
/** | |
* @var string | |
*/ | |
protected $description = 'Compile all view templates'; | |
/** | |
* @return void | |
*/ | |
public function handle(): void { | |
$this->call('view:clear'); | |
collect(Finder::create()->in(resource_path('views'))->files()) | |
->each([$this, 'compileView']); | |
$this->info('Views compiled successfully!'); | |
} | |
public function compileView(string $path) { | |
try { | |
Blade::compile($path); | |
} | |
catch (Throwable $exception) { | |
$this->error( | |
"Error compiling view $path\n" | |
. $exception->getMessage() | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment