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

时间:2015-06-29 08:49:04

标签: session laravel laravel-5

我正在尝试检查控制器中是否已设置会话密钥。文档说明可以检查数组中是否存在某个项目,就是这样。

http://laravel.com/docs/5.1/session

7 个答案:

答案 0 :(得分:10)

你可以使用

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

答案 1 :(得分:6)

@DavidDomain指出可能最好的方法是使用

With Cte as (some code) Select * from Cte order by id

对我来说就像一个魅力。

答案 2 :(得分:5)

你可以在刀片和控制器中使用Session :: has(' YOUR_SESSION_KEY')

控制器ex:

<?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 is not exist in the session
    }

  }
}
刀片ex:

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

答案 3 :(得分:2)

你可以这样做

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

答案 4 :(得分:1)

或更短,而不使用会话外观:if(session()->exists('key_here'))

答案 5 :(得分:0)

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

答案 6 :(得分:-2)

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

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

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

{{session->

@endif
相关问题