Search code examples
laravellaravel-artisanlumen

Clear views cache on Lumen


Few weeks ago, I had the same problem in Laravel 5.1, which I could solve with this solution.

However, now I'm facing the same issue in Lumen, but I can't call php artisan view:clear to clear the cached files. There is any other way?

Thanks!


Solution

  • There's no command for the view cache in lumen, but you can easily create your own or use my mini package found at the end of the answer.

    First, put this file inside your app/Console/Commands folder (make sure to change the namespace if your app has a different than App):

    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    
    class ClearViewCache extends Command
    {
    
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
    
        protected $name = 'view:clear';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Clear all compiled view files.';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            $cachedViews = storage_path('/framework/views/');
            $files = glob($cachedViews.'*');
            foreach($files as $file) {
                if(is_file($file)) {
                    @unlink($file);
                }
            }
        }
    }
    

    Then open app/Console/Kernel.php and put the command inside the $commands array (again, mind the namespace):

    protected $commands = [
            'App\Console\Commands\ClearViewCache'
    ];
    

    You can verify that everything worked by running

    php artisan
    

    inside the project's root.

    You will now see the newly created command:

    enter image description here

    You can now run it like you did in laravel.


    EDIT

    I've created a small (MIT) package for this, you can require it with composer:

    composer require baao/clear-view-cache
    

    then add

    $app->register('Baao\ClearViewCache\ClearViewCacheServiceProvider');
    

    to bootsrap/app.php and run it with

    php artisan view:clear