laravel 新版本问题使用route:cache的问题

1. 运行环境#

1). 当前使用的 Laravel 版本?#

Laravel Framework 10.35.0

2). 当前使用的 php/php-fpm 版本?#

PHP 版本:PHP 8.1.21

php-fpm 版本:PHP 8.1.21

3). 当前系统#

Ubuntu 22.04

4). 业务环境#

开发环境

2. 问题描述?#

我将版本降级回 10.20.0 是没有问题的,升级到 10.35.0 直接报错,下面是 web.php 路由文件

<?php
use Illuminate\Support\Facades\Route;

$log = app('log');
Route::any('{all}', function () use ($log) {

});

运行 php artisan route:cache 之后直接报错

3. 您期望得到的结果?#

INFO  Routes cached successfully.

4. 您实际得到的结果?#

 Exception 

  Serialization of 'WeakMap' is not allowed

  at vendor/laravel/framework/src/Illuminate/Routing/Route.php:1344
    1340public function prepareForSerialization()
    1341{
    1342if ($this->action['uses'] instanceof Closure) {
    1343$this->action['uses'] = serialize(1344SerializableClosure::unsigned($this->action['uses'])
    1345);
    1346}
    13471348if (isset($this->action['missing']) && $this->action['missing'] instanceof Closure) {

      +14 vendor frames 

  15  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

下面是版本截图

laravel 新版本问题使用route:cache的问题

laravel 新版本问题使用route:cache的问题

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

我也测试 10.35.0, 确实不行,这应该算是官方的 bug。

想用 10.35.0, 可以换个方式

Route::get('xxx',function() {
  $log = app('log');
});

或者:
$log = fn() => app('log');
Route::get('xxx',function() use ($log) {
    $log()->info('hello');
});
1年前 评论
silie (楼主) 1年前
讨论数量: 2

我也测试 10.35.0, 确实不行,这应该算是官方的 bug。

想用 10.35.0, 可以换个方式

Route::get('xxx',function() {
  $log = app('log');
});

或者:
$log = fn() => app('log');
Route::get('xxx',function() use ($log) {
    $log()->info('hello');
});
1年前 评论
silie (楼主) 1年前