You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel执行php artisan config:cache时报配置不可序列化错误的解决方法

问题描述

执行php artisan config:cache命令时,终端先输出Configuration cache cleared!,随后抛出LogicException异常,错误提示为Your configuration files are not serializable.,具体报错堆栈信息如下:

Configuration cache cleared!

LogicException

Your configuration files are not serializable.

at C:\xampp\htdocs\Pharmacy-management-system\vendor\laravel\framework\src\Illuminate\Foundation\Console\ConfigCacheCommand.php:71
67▕ require $configPath;
68▕ } catch (Throwable $e) {
69▕ $this->files->delete($configPath);
70▕ ➜
71▕ throw new LogicException('Your configuration files are not serializable.', 0, $e);
72▕ }
73▕
74▕ $this->info('Configuration cached successfully!'); 75▕ }

1 C:\xampp\htdocs\Pharmacy-management-system\bootstrap\cache\config.php:173 Error::("Call to undefined method Closure::__set_state()")

2 C:\xampp\htdocs\Pharmacy-management-system\vendor\laravel\framework\src\Illuminate\Foundation\Console\ConfigCacheCommand.php:67 require()

已尝试删除vendor文件夹并重新安装Composer,问题仍未解决,求修复方案。

修复方法

从报错信息Call to undefined method Closure::__set_state()可确定,问题核心是配置文件中存在无法序列化的匿名函数(Closure)。Laravel执行config:cache时会序列化所有配置内容,匿名函数不支持序列化,因此触发异常。按以下步骤修复:

  • 定位问题配置文件
    打开bootstrap/cache/config.php文件,查看第173行附近的代码,这里会指向包含匿名函数的原始配置文件(比如config/app.php、config/services.php或自定义配置文件),找到对应配置项。

  • 替换匿名函数
    将配置中的匿名函数改为可序列化的形式:

    • 示例:若配置中有如下代码:
      'some_key' => function () {
          return env('SOME_VAR', 'default_value');
      },
      
      直接修改为:
      'some_key' => env('SOME_VAR', 'default_value'),
      
    • 若配置依赖动态逻辑,将逻辑迁移至服务提供者的boot或register方法中,不要直接写在配置文件内。
  • 重新执行缓存操作
    先清理旧缓存:

    php artisan config:clear
    

    再重新生成配置缓存:

    php artisan config:cache
    
  • 后续注意事项
    编写配置文件时,仅使用数组、字符串、数字、布尔值等可序列化的基础类型,避免使用匿名函数、对象实例等无法序列化的内容。

内容的提问来源于stack exchange,提问作者Lina Boukhari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:13:14