检查Laravel 5.1中是否存在会话密钥?

3pmvbmvn  于 2023-02-05  发布在  其他
关注(0)|答案(9)|浏览(117)

我试着去检查会话密钥是否已经在控制器中设置好了。文档说明可以检查一个项目是否存在于数组中,仅此而已。
http://laravel.com/docs/5.1/session

oknwwptz

oknwwptz1#

您可以使用

if($request->session()->has('key'))
{
}
2jcobegt

2jcobegt2#

DavidDomain指出,最好的方法可能是使用
if(Session::has('...'))
对我来说就像一种魔力。

jtw3ybtb

jtw3ybtb3#

您可以在刀片和控制器中使用Session::has('YOUR_SESSION_KEY')
控制器,例如:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Session;

class add_classController extends Controller
{
  public function index(){
    if (Session::has('YOUR_SESSION_KEY')){
      // do some thing if the key is exist
    }else{
      //the key does not exist in the session
    }

  }
}

刀片,例如:

@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}}
@else
{{-- session key dosen't exist  --}}
@endif
svujldwt

svujldwt4#

你能做到的

if(Session::has('your_key')){
        return $next($request);
    }
gg0vcinb

gg0vcinb5#

if($request->session()->exists('your_key'){
// 
}
lx0bsm1f

lx0bsm1f6#

或更短,不使用会话虚包:if(session()->exists('key_here'))

bbmckpt7

bbmckpt77#

这对我有用

@if (session()->has('hasCoupon'))
    [{{ Session::get('hasCoupon')['name'] }}]</span>
@endif
dtcbnfnu

dtcbnfnu8#

@if ( session::has('message') 

{{ session::get('message') }}
@endif

@if (session->has->('message'))

{{session->

@endif
8yparm6h

8yparm6h9#

尝试以下新的laravel语法
@if(会话()-〉已(“成功”))

@endif

相关问题