You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel多语言切换时表单验证报array_replace_recursive类型错误

Laravel多语言表单验证报错解决:array_replace_recursive(): Argument #2 must be of type array, int given

问题描述

基于Laravel框架实现表单验证,创建了CreateEntrepriseForm请求类,默认英文环境下功能正常,但切换为法语后,语言切换中间件Language抛出错误:array_replace_recursive(): Argument #2 must be of type array, int given,需修复该问题以确保表单在所有语言环境下正常工作。

相关代码

表单验证类CreateEntrepriseForm

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateEntrepriseForm extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'name_entreprise' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'name_entreprise.required' => __('main.enter_your_company_name_please'),
        ];
    }
}

控制器处理方法

public function saveEntreprise(CreateEntrepriseForm $requestF)
{
     $name_entreprise = $requestF->input('name_entreprise');

     dd($name_entreprise);
}

语言切换中间件Language

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\App;

class Language
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if(Session()->has('applocale') AND array_key_exists(Session()->get('applocale'), config('languages')))
        {
            App::setLocale(Session()->get('applocale'));
        }
        else
        {
            App::setLocale(config('app.fallback_locale'));
        }

        return $next($request);
    }
}

错误截图

错误截图

问题原因

该错误由array_key_exists()函数的第二个参数类型非法导致——config('languages')返回的值不是数组,或者其中某个语言项的配置值为整数而非数组。

解决方案

1. 修正语言配置文件

检查config/languages.php,确保所有语言项的配置值为数组格式,示例:

return [
    'en' => [
        'name' => 'English',
        'locale' => 'en_US'
    ],
    'fr' => [
        'name' => 'Français',
        'locale' => 'fr_FR'
    ]
];

2. 确保翻译文件存在且格式正确

确认resources/lang/fr/main.php已创建,且目标翻译项为字符串:

return [
    'enter_your_company_name_please' => 'Veuillez entrer le nom de votre entreprise',
];

3. 优化中间件的安全检查

在中间件中增加类型校验,避免非数组配置引发错误:

public function handle(Request $request, Closure $next): Response
{
    $languages = config('languages');
    
    // 确保语言配置是合法数组
    if (!is_array($languages)) {
        App::setLocale(config('app.fallback_locale'));
        return $next($request);
    }

    if(Session()->has('applocale') && array_key_exists(Session()->get('applocale'), $languages))
    {
        App::setLocale(Session()->get('applocale'));
    }
    else
    {
        App::setLocale(config('app.fallback_locale'));
    }

    return $next($request);
}

内容的提问来源于stack exchange,提问作者Horly Andelo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:34:53