PHP artisan view:clear command clears whole compiled views in an application. How to clear compiled output for specific view.
1 Answers
Simple answer: Write your own command.
How do i start? First of all, You must know that compiled views have different names than the original blade views.
What names do they have? Laravel calls sha1() in the full file path. So for example. The compiled file name of layouts/app.blade.php (comes with default installation).
in versions less than 5.2 md5() is used instead of sha1(),
5.2, 5.3 => sha1()
5.1, 5.0, 4.2, 4.1, 4.0 => md5()
Assuming your version is >= 5.2
sha1('C:\xampp\htdocs\myapp\resources\views/layouts/app.blade.php');
So file name will be 9407584f16494299da8c41f4ed65dcb99af82ae2.php
How do i do that then?
- Create new command that takes filename as an argument.
- Add views path for filename in fire() function. As i showed you before
C:\xampp\htdocs\myapp\resources\views
(view full path) +/layouts/app.blade.php
(filename)
$path = 'C:\xampp\htdocs\myapp\resources\views' . '/layouts/app.blade.php';
$path = sha1($path) . '.php';
To get the compiled filename.- Check if filename exists in compiled views dir
- Delete file if exists
The command you'll have something like,
Note: If you have different view paths (Changed defaults), You must make changes on my code below.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use RuntimeException;
class RemoveCompiled extends Command
{
protected $signature = 'view:clearOne {file}';
protected $description = 'Remove one compiled view!';
public function handle()
{
$path = sha1($this->laravel['config']['view.paths'][0] . '/' . $this->argument('file'));
$f = $this->laravel['config']['view.compiled'] . '\\'. $path . '.php';
if(!file_exists($f))
return; //do whatever you want
if(unlink($f))
echo "File deleted!";
}
}
Calling: php artisan view:clearOne layouts/app.blade.php

- 1,848
- 2
- 15
- 23
-
Thanks, Seems like this will work, But my problem is some thing different, I have developed custom blade directive and i am returning a something dynamic string from it, lets say some db queried values, Now what happens the blade where i have added my custom directive does not reflects new values from DB , UNless i clear compiled view for that view ( which contains custom directive ) , I think it would not be good idea to clear all compiled views, Each time. – harish Oct 24 '16 at 05:53
-
@harish You call artisan commands within the code. So let's see, you query and return view with query result. before returning view. you can call artisan within the same method. and then return the view. – devnull Oct 24 '16 at 20:04
-
Yes i understood, But is it only way ? Coz then i think for each http request my views will get compiled freshly...will this affect application performance – harish Oct 24 '16 at 20:11
-
@harish "is it only way" That will start a new question far-away from "How do i delete one compiled view instead of all views". – devnull Oct 24 '16 at 20:36
-
Yes agreed.. Thanks , I appreciate your time and inputs (y) – harish Oct 24 '16 at 20:37