php 检查刀片模板中是否存在会话 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34498043/
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
Check if session exists inside blade template - Laravel
提问by moh_abk
Currently I have the below code in one of my vanilla php
files
目前我的一个香草php
文件中有以下代码
<?php
if (isset($_SESSION['qwick'])) {
include "checkout-lin.php";
} else {
include "checkout-nlin.php";
}
?>
How can I do the same in 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
我如何在 Laravel 中做同样的事情。我创建了2个文件我的includes
文件夹命名loggedinclude.blade.php
,并notloggedinclude.blade
和我现在想增加他们在一个页面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. and now I want to be able to check if a session named blog has been set. blog then has variable email etc
从上面的代码中,我创建了一个数组,然后将该数组放入一个名为 blog 的会话中,从而设置了一个名为 blog 的会话。现在我希望能够检查是否已设置名为 blog 的会话。博客然后有可变的电子邮件等
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')) {
//
}
回答by Erik
回答by mohammadreza gohari
you can use this way for Laravel : In your Controller or Route function:
您可以将这种方式用于 Laravel :在您的控制器或路由功能中:
$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
回答by Venkat.R
Try the below Code Snippet.
试试下面的代码片段。
@if(session->has('qwick'))
@include('includes.loggedinclude checkout-lin')
@else
@include('checkout-nlin')
@endif
Reference:
参考:
Laravel Session variables in Blade @if
http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template
http://laravel-recipes.com/recipes/90/include-a-blade-template-within-another-template
回答by gdmanandamohon
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. Thanks.
检查用户的更好方法是登录到控制器中。在视图部分检查用户登录可能会违反 MVC 规则。谢谢。
If you must need you can try this
如果你必须需要你可以试试这个
@if(session->get('qwick'))
@include("includes.loggedinclude")
@else
@include("notloggedinclude.loggedinclude")
@endif