【发布时间】:2015-09-15 15:55:07
【问题描述】:
我正在尝试检查是否已在控制器中设置会话密钥。文档指出,可以检查一个项目是否存在于数组中,仅此而已。
【问题讨论】:
-
你不能只做
if (Session::has('your_key'))吗?
我正在尝试检查是否已在控制器中设置会话密钥。文档指出,可以检查一个项目是否存在于数组中,仅此而已。
【问题讨论】:
if (Session::has('your_key')) 吗?
你可以使用
if($request->session()->has('key'))
{
}
【讨论】:
@DavidDomain 指出可能最好的方法是使用
if(Session::has('...'))
对我来说就像一个魅力。
【讨论】:
你可以在刀片和控制器中使用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
【讨论】:
你可以这样做
if(Session::has('your_key')){
return $next($request);
}
【讨论】:
if($request->session()->exists('your_key'){
//
}
【讨论】:
或者更短,不使用会话外观:if(session()->exists('key_here'))
【讨论】:
@if ( session::has('message')
{{ session::get('message') }}
@endif
或
@if (session->has->('message'))
{{session->
@endif
【讨论】:
这对我有用
@if (session()->has('hasCoupon'))
[{{ Session::get('hasCoupon')['name'] }}]</span>
@endif
【讨论】: