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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31111392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:03:37  来源:igfitidea点击:

Check if session key exists in Laravel 5.1?

sessionlaravellaravel-5

提问by AlGallaf

I'm trying to check it a session key has already been set inside a controller. The documentation states that it is possible to check if an item exists in an array and that's it.

我正在尝试检查它是否已在控制器中设置了会话密钥。该文档指出,可以检查数组中是否存在项目,仅此而已。

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

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

回答by Tariqul Islam

you can use

您可以使用

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

回答by Baldráni

Has pointed out by @DavidDomain probably the best way of doing it is to use

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

if(Session::has('...'))

if(Session::has('...'))

Worked like a charm for me.

对我来说就像一种魅力。

回答by Fouad Mekkey

you can use Session::has('YOUR_SESSION_KEY')in both blade and controller

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

controller 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 does not exist in the session
    }

  }
}

blade ex:

刀片例如:

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

回答by Sunjid Shibly

You can do this

你可以这样做

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

回答by Seema

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

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

回答by eskimo

Or shorter, without using the Session Facade: if(session()->exists('key_here'))

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