Blade @if 中的 Laravel 会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29649460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Session variables in Blade @if
提问by Fixspec
I've hit some weirdness with Laravel 4.2 when trying to set a JS variable in a blade using Laravel Session to flash some data. It's pretty simply, and I can't figure out what I've missed...
我在 Laravel 4.2 中尝试使用 Laravel Session 在刀片中设置 JS 变量来刷新一些数据时遇到了一些奇怪的问题。这很简单,我无法弄清楚我错过了什么......
Goal: trigger a Javascript website tour right after a user registers. Approach: Flash a welcome_tour variable to session, which then sets a JS boolean when the page refreshes which in turn starts the tour in JS.
目标:在用户注册后立即触发 Javascript 网站浏览。方法:将一个welcome_tour 变量刷入会话,然后在页面刷新时设置一个JS 布尔值,从而在JS 中启动游览。
My register function concludes with the Session setting:
我的注册功能以会话设置结束:
Session::flash('welcome_tour', TRUE);
And I'm testing that it's picked up and sets the JS variable correctly with the following in my blade:
我正在测试它是否被拾取并在我的刀片中正确设置了 JS 变量:
<script>
@if(Session::has('welcome_tour'))
var welcome_tour = true;
@else
var welcome_tour = false;
@endif
</script>
{{ var_dump(Session::all()) }}
The JS snippet in the resulting HTML indicates that the welcome_tour was not found :-(
结果 HTML 中的 JS 片段表明未找到welcome_tour :-(
<script>
var welcome_tour = false;
</script>
But the Session detail printed to the screen by the var_dump indicates that it was set correctly:
但是 var_dump 打印到屏幕上的 Session 详细信息表明它设置正确:
array (size=4)
'_token' => string 'HHBowu6wdYlIKy4kFiWYwCe4lQRkFbG7jdNDlYXQ' (length=40)
'flash' =>
array (size=2)
'old' =>
array (size=1)
0 => string 'welcome_tour' (length=12)
'new' =>
array (size=0)
empty
'login_82e5d2c56bdd0811318f0cf078b78bfc' => string '1137' (length=4)
'welcome_tour' => boolean true
How can the Session variable be both there and not there??
Session 变量如何既存在又不存在?
回答by Safoor Safdar
Try to use something like that.
尝试使用类似的东西。
<script type="text/javascript">
<?php
if(Session::has('welcome_tour')){
echo "welcome_tour = true";
}else{
echo "var welcome_tour = false";
}
?>
</script>
and also try dd(Session::has('welcome_tour'))
to verify the output to condition. might be session has method returning false.
并尝试dd(Session::has('welcome_tour'))
验证输出是否符合条件。可能是 session 有方法返回 false。