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(1344▕                 SerializableClosure::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 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

我也测试 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');
});
8个月前 评论
silie (楼主) 8个月前
讨论数量: 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');
});
8个月前 评论
silie (楼主) 8个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!