简体   繁体   English

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

[英]Check if session exists inside blade template - Laravel

Currently I have the below code in one of my vanilla php files目前我的一个 vanilla php文件中有以下代码

<?php 

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

?>

How can I do the same in laravel.我怎样才能在 laravel 中做同样的事情。 I have created 2 files in my includes folder named loggedinclude.blade.php and notloggedinclude.blade and I now want to add them in a page checkstatus.blade.php我在名为loggedinclude.blade.phpnotloggedinclude.bladeincludes文件夹中创建了 2 个文件,现在我想将它们添加到页面checkstatus.blade.php

Also I'm able to set a session like this我也可以像这样设置会话

$vars = [
    "email" => $email,
    "password" => $password,
    "firstname" => $row['firstName'],
    "lastname" => $row['lastName'],
    "id" => $row['id']
];

session()->put('blog', $vars);

From the above code, I'm creating an array then putting the array in a session called blog thereby setting the a session called blog.从上面的代码中,我创建了一个数组,然后将该数组放入一个名为 blog 的会话中,从而设置一个名为 blog 的会话。 and now I want to be able to check if a session named blog has been set.现在我希望能够检查是否已设置名为 blog 的会话。 blog then has variable email etc博客然后有可变的电子邮件等

FYI;供参考;

my first question is checking for session exist in blade我的第一个问题是检查刀片中是否存在会话

my second question is checking for named session exist in controller我的第二个问题是检查控制器中是否存在命名会话

The documentation only has code for checking session items文档只有检查session项的代码

if ($request->session()->has('users')) {
    //
}
@if(session()->has('qwick'))
    @include('includes.loggedinclude')
@else 
    @include('notloggedinclude.loggedinclude')
@endif

Documentation about blade can be found here .可以在此处找到有关刀片的文档。 Session documentation can be found here .可以在此处找到会话文档。

you can use this way for Laravel: In your Controller or Route function:你可以在 Laravel 中使用这种方式:在你的 Controller 或 Route 函数中:

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

and in Resource/Views/your_blade_file.blade.php在 Resource/Views/your_blade_file.blade.php

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

The better way to check out the user is logged inside the controller.检查用户的更好方法是在控制器内登录。 Checking user log in in view section may be break the MVC rule.在视图部分检查用户登录可能会破坏 MVC 规则。 Thanks.谢谢。

If you must need you can try this如果你必须需要你可以试试这个

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

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

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