比方時候登陸之后的用戶類,登陸之后,可以在任意地方調(diào)用這個類,獲取已登錄的信息?
你說的東西叫session,
你要搞清楚,http是短鏈接,請求的生命周期在你返回響應以后已經(jīng)結(jié)束,webman雖然在運行,但那是webman的生命周期,不是你這個請求的生命周期.所以,當下一次請求上來的時候,除非它攜帶cookie或者你自定義的校驗參數(shù)上來,不然根本不知道這個請求是誰.
我之前寫了一個 群主不讓我合并到主框架 給你參考一下吧
<?php
namespace Webman\Auth;
use Webman\App;
class AuthManger
{
/**
* 登錄頁地址
*/
protected $login_url;
/**
* 登錄成功后跳轉(zhuǎn)地址
*/
protected $return_url;
/**
* session中保存用戶ID的鍵名
*/
protected $user_id_key;
/**
* 當前用戶的認證類
*/
protected $identityClass;
private $_identity;
public function __construct($guard='web')
{
$config = config("auth.guards.{$guard}");
//默認用戶組不配置則使用默認配置
if( $guard=='web' && is_null($config) ){
$config = self::default();
}
if( $config && is_array($config) ){
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
}
/**
* 默認用戶組配置
*/
public static function default()
{
return [
'user_id_key' => '6ac21fd8270b4f6d_id',
'login_url' => 'login',
'return_url' => '/?login_success',
'identityClass' => '\app\model\User',
];
}
/**
* 指定看守器
*/
public function guard(string $guard)
{
return (new self($guard));
}
/**
* 返回當前看守器用戶儲存在session中的key
*/
protected function get_login_id()
{
return $this->user_id_key;
}
/**
* 登錄
*/
public function login($user)
{
return App::request()->session()->set($this->get_login_id(), $user->id());
}
/**
* 退出
*/
public function logout()
{
return App::request()->session()->forget($this->get_login_id());
}
/**
* 是否為游客
* @return 布爾值
*/
public function IsGuest()
{
return $this->user() === null;
}
/**
* 強制登錄 否則跳轉(zhuǎn)至登錄頁面
* webman中無法使用redirect
*/
public function loginRequired()
{
if( $this->IsGuest() ){
return redirect('/');
}
}
/**
* 返回用戶ID
*/
public function id()
{
if( $this->_identity == false ){
$this->renewAuthStatus();
}
return $this->_identity->id();
}
/**
* 獲取用戶類實例
*/
public function user()
{
if( $this->_identity == false ){
$this->renewAuthStatus();
}
return $this->_identity;
}
protected function renewAuthStatus()
{
$id = App::request()->session()->get($this->get_login_id());
//讀取用戶信息
if( $id == null ){
$identity = null;
}else{
$class = $this->identityClass;
$identity = $class::findIdentity($id);
}
//驗證validateAuthKey
if ($identity !== null){
}
$this->setIdentity($identity);
}
protected function setIdentity($identity)
{
if( $identity instanceof \Webman\Auth\IdentityInterface ){
$this->_identity = $identity;
}elseif ($identity === null){
$this->_identity = null;
}else{
throw new \Exception("identity must implements from IdentityInterface");
}
}
}
Support下建一個單例類
<?php
namespace support;
use Webman\Auth\AuthManger;
class Auth
{
public static function __callStatic($method, $args)
{
$request = request();
if( !$request->auth_instance ){
$request->auth_instance = new AuthManger();
}
$instance = $request->auth_instance ;
if (! $instance) {
throw new RuntimeException('未獲取到 Webman\Auth\Auth 的實例');
}
return $instance->$method(...$args);
}
}
User模型
<?php
namespace app\model;
use support\Model;
use Webman\Auth\IdentityInterface;
class User extends Model implements IdentityInterface
{
protected $table = 'user';
public function id()
{
return $this->id;
}
public static function findIdentity($id)
{
return self::where('id', $id)->first();
}
public static function findByLoginName($login_name)
{
if( filter_var($login_name, FILTER_VALIDATE_EMAIL) ){
$login_field = 'email';
}else{
$login_field = 'mobile';
}
return self::where($login_field, $login_name)->first();
}
public function validatePassword($password)
{
return $this->password == $password;
}
public function setPassword($password)
{
return $this->password = md5($password);
}
}
命名空間你按自己放的位置和需求改一下吧
//用法
use support\Auth;
Auth::login($user)
Auth::logout()
if( !Auth::IsGuest() ){
return Auth::user()->username
}
Auth::guard('admin')->login($user)
樓上有位老哥說的是對的,就是請求生命周期單例。
你可以放到request
里面,例如:
.
.
.
$data = 123;
$request->data = $data;
然后在業(yè)務代碼里獲取request
就行
直接中間件處理...在中間件中讀取請求中的TOKEN,緩存讀取用戶信息,存到request上,進行讀取.如果是其他的類,可以使用DI依賴注入或者直接靜態(tài)類。(謹慎使用靜態(tài)變量)