我遇到问题,文件无法找到变量'lang'。
{{ Form::open(['action' => 'loon.language']) }}
{{Form::select('lang',['nl'=>'nl','po'=>'po'], $lang,['onchange'=>'submit()'])}}
{{$lang = 'blaat'}}
{{var_dump($lang)}}
{{ Form::close()}}
控制器:
public function postChangeLanguage()
{
$rules = [
'language' => 'in:nl,po' //list of supported languages of your application.
];
$language = Input::get('lang'); //lang is name of form select field.
$validator = Validator::make(compact($language),$rules);
// $language = Session::get('language',Config::get('app.locale'));
if($validator->passes())
{
Session::put('language',$language);
App::setLocale($language);
}
else
{ /**/ }
}
路线:
Route::get('language', array(
'uses' =>'LoonController@postChangeLanguage',
'as' => 'loon.language'
));
Filter.php:
App::before(function($request)
{
$language = Session::get('language','nl'); //en will be the default language.
App::setLocale($language);
});
我甚至试图调试它仍然是这个错误代码!
未定义变量:lang(查看:/Users/nielsvandijk/loon/rekentool/app/views/partials/header.blade.php)(查看:/ Users / nielsvandijk / loon / rekentool / app / views / partials / header .blade.php)
有人可以帮忙吗?
答案 0 :(得分:1)
你第一次使用$ lang时显然仍然是空的.. 快速解决方法是在它前面使用@ sing(允许它未定义)
{{Form::select('lang',['nl'=>'nl','po'=>'po'], @$lang,['onchange'=>'submit()'])}}
lang变量的目的是在选择字段
中定义“selected”元素{{Form::select('lang',['nl'=>'nl','po'=>'po'], 'po',['onchange'=>'submit()'])}}
这将导致:
<select name="lang">
<option value="nl">nl</option>
<option value="po" selected="selected">po</option>
</select>
在控制器中,如果没有预先选择语言,您可以设置默认语言
View::make('view')->with('lang','nl');