Laravel htmlspecialchars()期望参数1为字符串,给定数组 [英] Laravel htmlspecialchars() expects parameter 1 to be string, array given

查看:76
本文介绍了Laravel htmlspecialchars()期望参数1为字符串,给定数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在配置文件夹中获取 languages.php 提供的语言列表,但出现此错误

I try to get list of language which provided by languages.php in my config folder and I get this error

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\laragon\www\newproject\resources\views\layouts\panel.blade.php) (View: C:\laragon\www\newproject\resources\views\layouts\panel.blade.php)

这是我的 languages.php

<?php

return [
  'en' => [
      'name' => 'English',
      'flag' => 'images/flags/en.png'
  ],
  'fa' => [
      'name' => 'پارسی',
      'flag' => 'images/flags/iran.png'
  ],

];

我的中间件 Language.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;


class Language
{
    public function handle($request, Closure $next)
    {
      if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
          App::setLocale(Session::get('applocale'));
      }
      else { // This is optional as Laravel will automatically set the fallback language if there is none specified
          App::setLocale(Config::get('app.fallback_locale'));
      }
      return $next($request);
    }
}

LanguageController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class LanguageController extends Controller
{
    public function index($lang)
    {
      if (array_key_exists($lang, Config::get('languages'))) {
            Session::put('applocale', $lang);
        }
        return Redirect::back();
    }
}

路线

Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LanguageController@index']);

最后是我的观点

<li class="dropdown langs">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
  {{ Config::get('languages') }} <span class="caret"></span>
</a>

 <ul class="dropdown-menu" role="menu">
   @foreach (Config::get('languages') as $lang => $language)
   @if ($lang != App::getLocale())
   <li>
     <a href="{{ route('lang.switch', $lang) }}">{{ $language['name'] }}</a>
   </li>
   @endif
   @endforeach
 </ul>
</li>

推荐答案

错误位于下拉列表定义的此行 {{Config :: get('languages')}} 上,在此您希望打印一个字符串,而不是像字符串一样尝试打印从 languages.php 获取的完整数组,因此 htmlspecialchars 函数失败,因为第一个参数与所需的参数类型不匹配

The error is on this line {{ Config::get('languages') }} on the dropdown definition, here you wish to print a string instead you are trying to print the full array taken from languages.php like a string hence the htmlspecialchars function fails because the first argument mismatch the required argument type.

这篇关于Laravel htmlspecialchars()期望参数1为字符串,给定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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