国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

請問如何在一個請求的生命周期里復用類?

ersic

比方時候登陸之后的用戶類,登陸之后,可以在任意地方調(diào)用這個類,獲取已登錄的信息?

2252 5 1
5個回答

2548a

你說的東西叫session,
你要搞清楚,http是短鏈接,請求的生命周期在你返回響應以后已經(jīng)結(jié)束,webman雖然在運行,但那是webman的生命周期,不是你這個請求的生命周期.所以,當下一次請求上來的時候,除非它攜帶cookie或者你自定義的校驗參數(shù)上來,不然根本不知道這個請求是誰.

  • ersic 2022-05-12

    嗯,可能想岔了。其實就是看到 laravel 可以在Auth::login 后,隨意在代碼其他地方獲取用戶信息,不知道 webman 怎么實現(xiàn)??

  • walkor 2022-05-12

    自己實現(xiàn)一個類或者函數(shù)取下session就好了,例如app/functions.php里定義一個函數(shù)

    function user()
    {
        // 假設登錄用戶會寫一個user_info為key的session信息
        return session('user_info');
    }
  • ersic 2022-05-12

    好的,多謝!

liziyu

beforeAction() 配合中間件實現(xiàn),應該可以。
我沒試過!

  • 極勝100 2022-05-12

    其實就是單例,或者 static,但是webman中記得請求完成后手動清理掉

aphper

我之前寫了一個 群主不讓我合并到主框架 給你參考一下吧

<?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)
  • ersic 2022-05-12

    ok,我參考一下,多謝

tegic

樓上有位老哥說的是對的,就是請求生命周期單例。
你可以放到request里面,例如:

.
.
.
$data = 123;
$request->data = $data;

然后在業(yè)務代碼里獲取request就行

  • 暫無評論
橘叔

直接中間件處理...在中間件中讀取請求中的TOKEN,緩存讀取用戶信息,存到request上,進行讀取.如果是其他的類,可以使用DI依賴注入或者直接靜態(tài)類。(謹慎使用靜態(tài)變量)

  • 暫無評論
年代過于久遠,無法發(fā)表回答
??