Laravel framework(php)中不允许序列化'Closure'[duplicate]

zpgglvta  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(502)

此问题已在此处有答案

Exception: Serialization of 'Closure' is not allowed(5个答案)
15天前关闭。
我得到这个错误时,我想登录在我的会议,我不知道我如何解决它,我需要它,因为明天我的毕业。第一行中的错误:$response = $kernel->handle($request = Request::capture())->send();

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
   require $maintenance;
}

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Kernel::class);

$response = $kernel->handle(
   $request = Request::capture()
)->send();

$kernel->terminate($request, $response);

最后一行的第二个错误

<?php


$publicPath = getcwd();
$uri = urldecode(

    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? ''

);
if ($uri !== '/' && file_exists($publicPath.$uri)) {

    return false;

}
require_once $publicPath.'/index.php';

这是authClass

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function getLogin()
    {
        return view('admin.auth.login');
    }
    public function postLogin(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required'
        ]);
        $validated = auth()->attempt([
            'email' => $request->email,
            'password' => $request->password,
            'is_admin' => 1
        ], $request->password);
        if ($validated) {
            return redirect()->route('dashboard')->with('success', 'Login Successfull');
        } else {
            return redirect()->back()->with('error', $request);
        }
    }
}

这一行的写法是:return redirect()->back()->with('error','Invalid Credentials');我改只知道错误

fd3cxomn

fd3cxomn1#

在Laravel中,默认情况下不允许闭包的序列化。存在这种限制是因为闭包是匿名函数,可以包含对周围上下文和变量的引用。序列化闭包及其周围的状态可能很复杂且容易出错。
当你尝试在Laravel中序列化一个闭包时,你可能会遇到类似这样的错误:
Serialization of 'Closure' is not allowed
要解决此限制,可以重构代码以避免序列化闭包。相反,您可以使用其他可序列化类型,例如类或数据结构(如数组或对象)。
如果需要存储某些状态或行为,可以创建一个类并定义所需的方法或属性。然后,您可以序列化和反序列化该类的示例,而不会出现任何问题。
下面是一个重构代码的例子,它将闭包使用到类中:

class MySerializableClass
{
    public function doSomething()
    {
        // Your code here
    }
}

$serializableInstance = new MySerializableClass();

// Serialize the instance
$serialized = serialize($serializableInstance);

// Unserialize the instance
$unserialized = unserialize($serialized);

// Call the method on the unserialized instance
$unserialized->doSomething();

通过使用可序列化类,您可以安全地序列化和反序列化示例,而不会遇到任何与闭包相关的问题。
请记住,闭包是PHP中的一个强大功能,但在序列化方面有其局限性。重构你的代码来使用可序列化的类可以帮助你克服Laravel中的这个限制。

相关问题