laravel:php artisan优化命令返回“不可序列化"错误

编程入门 行业动态 更新时间:2024-10-28 16:29:18
本文介绍了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优化命令返回“不可序列化"错误

本文发布于:2023-10-25 14:37:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   错误   序列化   php   laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!