4

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!

Community
  • 1
  • 1
henriale
  • 1,012
  • 9
  • 21

1 Answers1

5

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

baao
  • 71,625
  • 17
  • 143
  • 203
  • @baao Do you have any idea about php artisan optimize ? it is not available in Lumen, I am using Lumen and want to improve performance, please share your idea.Thanks much! – Sachin Vairagi Aug 24 '16 at 10:28
  • 1
    @baao I added the package and registered it, but am getting this error when I attempt the artisan command: 'Call to undefined method Laravel\Lumen\Application::share()' referencing this line: 'vendor/baao/clear-view-cache/src/ClearViewCacheServiceProvider.php on line 17'. I'm using Lumen 5.4.x Can you help? (And thanks for the package!) – Gary Apr 04 '17 at 22:52
  • 1
    @Gary the package was written for lumen 1 or 2, so most likely something has changed since then. I didn't work with laravel or lumen for ages, so I'm afraid I can't really tell what the error is. The package only consists of 2 files though, maybe have a look in the ServiceProvider and replace the whole register method with `public function register() { $this->app['command.view.clear'] = function () { return new ClearViewCache(); }; }` – baao Apr 05 '17 at 08:43
  • How about `view:cache`? – Sky Oct 10 '19 at 05:55