一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 4754|回复: 1
打印 上一主题 下一主题

php中lumen的自定义依赖注入示例介绍

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17424
QQ
跳转到指定楼层
楼主
发表于 2020-4-19 21:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
比如我现在有个token认证系统,目前我用mysql的token表实现,将来有可能会改成redis,怎么实现未来的无缝连接呢。

先定义一个合约文件app/Contracts/TokenHandler.php


<?php

namespace AppContracts;

/**
 * 处理Token的Contracts
 * @package AppContracts
 */
interface TokenHandler
{
    /**
     * 创建一个token
     * @param $userId integer 用户Id
     * @return string
     */
    public function createToken($userId);

    /**
     * 得到该token的用户
     * @param $token string token值
     * @return AppUser 拥有该token的用户
     */
    public function getTokenUser($token);

    /**
     * 删除一个token
     * @param $token string token值
     * @return bool 是否成功
     */
    public function removeToken($token);
}

  这里定义了3个方法:创建token,得到token对应用户,删除token。

然后我们写一个Mysql下的实现app/Services/MysqlTokenHandler.php


<?php

namespace AppServices;

use AppContractsTokenHandler;
use AppOrmToken;

/**
 * 处理Token的Contracts对应的Mysql Service
 * @package AppServices
 */
class MysqlTokenHandler implements TokenHandler
{
    /**
     * @var int 一个用户能够拥有的token最大值
     */
    protected $userTokensMax = 10;

    /**
     * @inheritdoc
     */
    public function createToken($userId)
    {
        while (Token::where('user_id', $userId)->count() >= $this->userTokensMax) {
            Token::where('user_id', $userId)->orderBy('updated_at', 'asc')->first()->delete();
        }

        $token = IlluminateSupportStr::random(32);
        if (!Token::create(['token' => $token, 'user_id' => $userId])) {
            return false;
        }

        return $token;
    }

    /**
     * @inheritdoc
     */
    public function getTokenUser($token)
    {
        $tokenObject = Token::where('token', $token)->first();

        return $tokenObject && $tokenObject->user ? $tokenObject->user : false;
    }

    /**
     * @inheritdoc
     */
    public function removeToken($token)
    {
        return Token::find($token)->delete();
    }
}

然后在bootstrap/app.php里绑定两者的映射关系:


$app->singleton(
    AppContractsTokenHandler::class,
    AppServicesMysqlTokenHandler::class);

如果将来换成了redis,只要重新写一个RedisTokenHandler的实现并重新绑定即可,具体的业务逻辑代码不需要任何改变。

于是在controller里就可以直接注入该对象实例,只要在参数前声明合约类型:


    public function logout(Request $request, TokenHandler $tokenHandler)
    {
        if ($tokenHandler->removeToken($request->input('api_token'))) {
            return $this->success([]);
        } else {
            return $this->error(Lang::get('messages.logout_fail'));
        }
    }

也可以在代码里手动得到注入对象的实例,比如:


$currentUser = app(AppContractsTokenHandler::class)->getTokenUser($request->input('api_token'));

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

0

主题

19

帖子

61

积分

注册会员

Rank: 2

积分
61
沙发
发表于 2022-9-26 18:47 来自手机 | 只看该作者
开源erp管理系统
回复

使用道具 举报

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表