2017-09-14 89 views
0

我試圖讓這由languages.php在我的配置文件夾中提供的語言列表,我得到這個錯誤Laravel用htmlspecialchars()預計參數1是字符串數組給定

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'=>'[email protected]']); 

最後我看來

<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> 
+0

問題在'layouts \ panel.blade.php'中,請分享此文件。 –

+0

@sunitiyadav我做了我的問題的最後一部分代碼是我的panel.blade.php – mafortis

回答

0

的錯誤是對的下拉定義這條線{{ Config::get('languages') }},在這裏你想打印一個字符串,而不是你試圖打印從字符串languages.php的整個陣列,因此htmlspecialchars函數失敗,因爲第一個參數不匹配所需的參數類型。

+0

好吧,我明白了,但我需要顯示選定的語言頂部,我該怎麼做? – mafortis

+0

@mafortis {{Config :: get('languages')[App :: getLocale()] ['name']}}將是一個選擇,您的應用程序始終有一個區域設置,您可以使用區域設置來確定配置語言作爲一個關鍵。 – asolenzal

相關問題