[英]How to use session in custom class laravel 5?
這是我的代碼:
<?php
use Illuminate\Support\Facades\Session;
namespace App\CustomLibrary
{
class myFunctions {
public function is_login() {
if(Session::get('id') != null){
return TRUE;
}
}
}
}
?>
我是laravel 5的新手,我剛剛添加了一個新的自定義功能。 在這個功能里面我想檢查一個會話('id)? 但我有這樣的錯誤
myFunctions.php第8行中的FatalErrorException:找不到類'App \\ CustomLibrary \\ Session'
我需要知道如何正確使用會話。
在namespace
部分之后,將此子句添加到類的頂部:
use Session;
或使用完整命名空間:
if (\Session::get('id') != null)
或者使用session()
幫助器:
if (session('id') != null)
您的use
必須在命名空間聲明后 :
//use Illuminate\Support\Facades\Session; //Not here
namespace App\CustomLibrary
use Illuminate\Support\Facades\Session; //Here
在命名空間之前use
任何內容都在全局命名空間中使用,該命名空間在聲明命名空間后會發生更改。
使用Session; 在模型和控制器中
//通過請求實例...
$ request-> session() - > put('key','value');
//通過全球助手......
session(['key'=>'value']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.