Blade @if中的Laravel Session变量 [英] Laravel Session variables in Blade @if
问题描述
当我尝试使用Laravel Session在刀片中设置JS变量来刷新一些数据时,我对Laravel 4.2感到有些奇怪.很简单,我不知道自己错过了什么...
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...
目标:在用户注册后立即触发Javascript网站访问. 方法:将welcome_tour变量刷新到会话中,然后在页面刷新时将其设置为JS布尔值,从而在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.
我的注册功能以会话设置结束:
My register function concludes with the Session setting:
Session::flash('welcome_tour', TRUE);
我正在测试它是否被拾取,并使用刀片中的以下内容正确设置JS变量:
And I'm testing that it's picked up and sets the JS variable correctly with the following in my blade:
<script>
@if(Session::has('welcome_tour'))
var welcome_tour = true;
@else
var welcome_tour = false;
@endif
</script>
{{ var_dump(Session::all()) }}
结果HTML中的JS代码段表明未找到welcome_tour:-(
The JS snippet in the resulting HTML indicates that the welcome_tour was not found :-(
<script>
var welcome_tour = false;
</script>
但是var_dump在屏幕上显示的会话详细信息表明设置正确:
But the Session detail printed to the screen by the var_dump indicates that it was set correctly:
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
Session变量如何既存在又不存在?
How can the Session variable be both there and not there??
推荐答案
尝试使用类似的方法.
<script type="text/javascript">
<?php
if(Session::has('welcome_tour')){
echo "welcome_tour = true";
}else{
echo "var welcome_tour = false";
}
?>
</script>
,然后尝试dd(Session::has('welcome_tour'))
验证输出是否符合条件.可能是会话的方法返回false.
and also try dd(Session::has('welcome_tour'))
to verify the output to condition. might be session has method returning false.
这篇关于Blade @if中的Laravel Session变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!