laravel:php artisan优化命令返回“不可序列化"错误 [英] laravel: php artisan optimize command return 'not serializable' error

查看:72
本文介绍了laravel:php artisan优化命令返回“不可序列化"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

laravel 6

laravel 6

当我运行时: php artisan优化

我收到此错误:

您的配置文件不可序列化.

Your configuration files are not serializable.

我检查并找到目标文件:

I checked and I found the target file:

错误: config \ sluggable.php 由于此部分代码:

error in: config\sluggable.php beacuse of this part of code:

 'method' => function($string, $separator = '-') {
      $_transliteration = ["/ö|œ/" => "e",
          "/ü/" => "e",
          "/Ä/" => "e",
          "/Ü/" => "e",
          "/Œ/" => "E",
          "/ƒ/" => ""];
      $quotedReplacement = preg_quote($separator, '/');
      $merge = [
          '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
          '/[\s\p{Zs}]+/mu' => $separator,
          sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
      ];
      $map = $_transliteration + $merge;
      unset($_transliteration);
      return preg_replace(array_keys($map), array_values($map), $string);
  },

我知道Laravel中不允许闭包序列化.但是如何在sluggable.php中使用传统函数重写这段代码?

I know Closure serialization is not allowed in Laravel. But how can I rewrite this piece of code using traditional functions in sluggable.php?

推荐答案

这不是Laravel的限制,而是Php.有 变通方法,但这取决于使用该"method"值的功能是否正在检查 Closure 的类型提示或内容是否仅需要可以被 callable .

This isn't a Laravel limitation, its a Php one. There is a workaround, but it kind of depends on whether the functionality using that 'method' value is checking for a type-hint of Closure or if the content merely needs to be callable.

创建一个新的类,并对其应用Magic __ invoke()方法.在该方法内部,执行该Closure并返回结果.并设置您的配置以引用该类的实例.这里的窍门是,序列化现在可以引用一个对象而不是一个函数,并且只要该函数不应用于该类中的属性,它就应该起作用,因为序列化不会折叠方法.然后, __ invoke()方法允许任何下游行为与对象进行通信,就好像它是函数一样.

Create a new Class, and apply the Magic __invoke() method to it. Inside that method, execute that Closure and return the result. And set your config to reference an instance of that class. The trick here is the serialization can reference an object instead of a function now, and so long as that function is not applied to a property within that class, it should work, because serialization doesn't collapse methods. The __invoke() method then allows whatever downstream behavior to communicate with the object as if it's a function.

尽管如此,我还是建议您尝试寻找一条完全不需要关闭的途径.如果存在该选项,它将更加干净.

I'd advise though, attempting to find a different avenue that doesn't require a Closure at all. If the option exists, it would be cleaner.

这篇关于laravel:php artisan优化命令返回“不可序列化"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆