检查刀片模板内是否存在会话 - Laravel

时间:2015-12-28 17:36:12

标签: php laravel laravel-5

目前,我的一个vanilla php文件中包含以下代码

<?php 

if (isset($_SESSION['qwick'])) {
    include "checkout-lin.php";
} else {
    include "checkout-nlin.php";
}

?>

我怎样才能在laravel中做同样的事情。我在名为includesloggedinclude.blade.php的{​​{1}}文件夹中创建了2个文件,现在我想将它们添加到页面notloggedinclude.blade

此外,我可以设置这样的会话

checkstatus.blade.php

从上面的代码中,我创建了一个数组,然后将该数组放入一个名为blog的会话中,从而设置了一个名为blog的会话。现在我希望能够检查是否已设置名为blog的会话。博客然后有可变电子邮件等

FYI;

我的第一个问题是检查刀片中是否存在会话

我的第二个问题是检查控制器中存在的命名会话

该文档仅包含用于检查$vars = [ "email" => $email, "password" => $password, "firstname" => $row['firstName'], "lastname" => $row['lastName'], "id" => $row['id'] ]; session()->put('blog', $vars); 项目

的代码
session

4 个答案:

答案 0 :(得分:5)

@if(session()->has('qwick'))
    @include('includes.loggedinclude')
@else 
    @include('notloggedinclude.loggedinclude')
@endif

有关刀片的文档可以找到here。 可以找到会话文档here

答案 1 :(得分:1)

您可以对Laravel使用这种方式: 在您的Controller或Route函数中:

$request->session()->flash('your_session', 'your message');

以及在Resource / Views / your_blade_file.blade.php

@if (session()->has('your_session'))
    anything ...
@endif

答案 2 :(得分:0)

尝试以下代码段。

@if(Session::has('qwick')) 
 @include('includes.loggedinclude checkout-lin')
@else
  @include('checkout-nlin')
@endif

参考:

Laravel Session variables in Blade @if

http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template

If statements in blade templating?

答案 3 :(得分:-1)

检查用户的更好方法是在控制器内部记录。检查用户登录视图部分可能会违反MVC规则。 感谢。

如果您需要,可以试试这个

@if(Session::get('qwick'))
@include("includes.loggedinclude")
@else 
@include("notloggedinclude.loggedinclude")
@endif
相关问题