【问题标题】:Clear views cache on Lumen清除 Lumen 上的视图缓存
【发布时间】:2015-11-24 21:42:54
【问题描述】:

几周前,我在 Laravel 5.1 中遇到了同样的问题,我可以通过 this solution 解决。

但是,现在我在 Lumen 中遇到了同样的问题,但我无法调用 php artisan view:clear 来清除缓存的文件。还有其他方法吗?

谢谢!

【问题讨论】:

    标签: laravel laravel-artisan lumen


    【解决方案1】:

    lumen 中没有用于视图缓存的命令,但您可以轻松创建自己的或使用答案末尾找到的我的迷你包。

    首先,将此文件放入您的 app/Console/Commands 文件夹中(如果您的应用与 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);
                }
            }
        }
    }
    

    然后打开app/Console/Kernel.php 并将命令放入$commands 数组中(再次注意命名空间):

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

    您可以通过运行验证一切正常

    php artisan
    

    在项目的根目录中。

    您现在将看到新创建的命令:

    你现在可以像在 laravel 中一样运行它了。


    编辑

    我为此创建了一个小型 (MIT) package,您可以通过 composer 要求它:

    composer require baao/clear-view-cache
    

    然后添加

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

    bootsrap/app.php 并使用

    运行它
    php artisan view:clear
    

    【讨论】:

    • @baao 你对 php artisan optimize 有什么想法吗?它在 Lumen 中不可用,我正在使用 Lumen 并希望提高性能,请分享您的想法。非常感谢!
    • @baao 我添加了包并注册了它,但是当我尝试使用工匠命令时出现此错误:'Call to undefined method Laravel\Lumen\Application::share()' 引用此行: '供应商/baao/clear-view-cache/src/ClearViewCacheServiceProvider.php 第 17 行'。我正在使用 Lumen 5.4.x 你能帮忙吗? (感谢您的包裹!)
    • @Gary 该软件包是为流明 1 或 2 编写的,因此从那时起很可能发生了一些变化。我已经很久没有使用 laravel 或 lumen 了,所以恐怕我无法真正说出错误是什么。该包仅包含 2 个文件,也许可以查看 ServiceProvider 并将整个注册方法替换为 public function register() { $this-&gt;app['command.view.clear'] = function () { return new ClearViewCache(); }; }
    • view:cache 怎么样?
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2016-05-30
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多