[英]Check if session exists inside blade template - Laravel
目前我的一個 vanilla php
文件中有以下代碼
<?php
if (isset($_SESSION['qwick'])) {
include "checkout-lin.php";
} else {
include "checkout-nlin.php";
}
?>
我怎樣才能在 laravel 中做同樣的事情。 我在名為loggedinclude.blade.php
和notloggedinclude.blade
的includes
文件夾中創建了 2 個文件,現在我想將它們添加到頁面checkstatus.blade.php
我也可以像這樣設置會話
$vars = [
"email" => $email,
"password" => $password,
"firstname" => $row['firstName'],
"lastname" => $row['lastName'],
"id" => $row['id']
];
session()->put('blog', $vars);
從上面的代碼中,我創建了一個數組,然后將該數組放入一個名為 blog 的會話中,從而設置一個名為 blog 的會話。 現在我希望能夠檢查是否已設置名為 blog 的會話。 博客然后有可變的電子郵件等
供參考;
我的第一個問題是檢查刀片中是否存在會話
我的第二個問題是檢查控制器中是否存在命名會話
文檔只有檢查session
項的代碼
if ($request->session()->has('users')) {
//
}
你可以在 Laravel 中使用這種方式:在你的 Controller 或 Route 函數中:
$request->session()->flash('your_session', 'your message');
在 Resource/Views/your_blade_file.blade.php
@if (session()->has('your_session'))
anything ...
@endif
試試下面的代碼片段。
@if(session->has('qwick'))
@include('includes.loggedinclude checkout-lin')
@else
@include('checkout-nlin')
@endif
http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template
檢查用戶的更好方法是在控制器內登錄。 在視圖部分檢查用戶登錄可能會破壞 MVC 規則。 謝謝。
如果你必須需要你可以試試這個
@if(session->get('qwick'))
@include("includes.loggedinclude")
@else
@include("notloggedinclude.loggedinclude")
@endif
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.