首页 > 解决方案 > 我收到错误 FatalThrowableError 类型错误:传递给 Illuminate\Http\Request::merge() 的参数 1 必须是数组类型,给定 null

问题描述

我想使用 Google Authenticator 制作一个具有两个因素身份验证的注册表单。当我尝试注册一个新用户帐户并扫描生成的二维码然后按完成注册按钮时,我收到了一个错误

FatalThrowableError 类型错误:传递给 Illuminate\Http\Request::merge() 的参数 1 必须是数组类型,给定 null,在 C:\xampp\htdocs\repository\app\Http\Controllers\Auth\RegisterController.php 中调用在第 113 行

这是我的注册控制器

public function register(Request $request)
    {
        //first we will validate the incoming request
        $this->validator($request->all())->validate();
        // Initialise the 2FA class
        $google2fa = app('pragmarx.google2fa');
        // store registration data in an array
        $registration_data = $request->all();
        // Create new secret key to the registration data
        $registration_data["google2fa_secret"] = $google2fa->generateSecretKey();
        // store registration data to the user session for new request
        $request->session()->flash('registration_data', $registration_data);
        // Create the QR image. 
        $QR_Image = $google2fa->getQRCodeInline(
            config('app.name'),
            $registration_data['email'],
            $registration_data['username'],
            $registration_data['google2fa_secret']
        );
        // Send the QR barcode image to our view
        return view('google2fa.register', ['QR_Image' => $QR_Image, 'secret' => $registration_data['google2fa_secret']]);
    }
    public function completeRegistration(Request $request) 
    {
        // add the session data back to the request input
        $request->merge(session('registration_data')); <- I got error because of this line (in line 113)
        // Call the default laravel authentication
        <br>return $this->registration($request);
    }

这是路由地址

Route::get('/complete-registration', 'Auth\RegisterController@completeRegistration');

我已经在上面的寄存器控制器中输入了方法 completeRegistration

标签: phplaravel

解决方案


使用if条件来检查是否registration_data为空。

if (session('registration_data') != '' && session('registration_data') != null)
{
    $request->merge(session('registration_data'));
}

推荐阅读