简体   繁体   English

Laravel 5.7在刀片中回显会话值

[英]Laravel 5.7 echo out session value in blade

I am trying to echo out the value of a session that I set in Controller 我试图回显我在Controller中设置的会话的值

public function changeLanguage(Request $request){
    if($request->ajax()){
        $request->session()->put('locale',$request->locale);
        $request->session()->flash('alert-success',('app.Locale_Change_Success'));
    } 
}

the Values of $request->local that submits by ajax are dr and en . ajax提交的$request->local的值是dren Now I want to the value of dr and en in blade, the code looks like this 现在我想知道dren在blade中的值,代码看起来像这样

@if(Session::has('locale','en'))
    <link rel="stylesheet" type="text/css" href="css/en.css">
@endif
@if(Session::has('locale','dr'))
    <link rel="stylesheet" type="text/css" href="css/dr.css">
@endif

According to the Laravel docs one way to check it could be this: 根据Laravel文档,一种检查方法可能是:

@if(session()->has('locale'))
    <link rel="stylesheet" type="text/css" href="css/{{ session()->get('locale') }}.css">
@endif

This saves you duplicate code (the <link> tag) and is easier to read. 这样可以节省重复的代码( <link>标记),并且更易于阅读。 It also saves you the hassle of checking for each possible locale you might add in the future. 它还为您节省了以后检查您可能添加的每个可能的语言环境的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM