浅谈lumen的自定义依赖注入

2025-05-29 0 77

比如我现在有个token认证系统,目前我用mysql的token表实现,将来有可能会改成redis,怎么实现未来的无缝连接呢。

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
<?php

namespace App\\Contracts;

/**

* 处理Token的Contracts

* @package App\\Contracts

*/

interface TokenHandler

{

/**

* 创建一个token

* @param $userId integer 用户Id

* @return string

*/

public function createToken($userId);

/**

* 得到该token的用户

* @param $token string token值

* @return \\App\\User 拥有该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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53
<?php

namespace App\\Services;

use App\\Contracts\\TokenHandler;

use App\\Orm\\Token;

/**

* 处理Token的Contracts对应的Mysql Service

* @package App\\Services

*/

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 = \\Illuminate\\Support\\Str::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里绑定两者的映射关系:

?

1

2

3

4
$app->singleton(

App\\Contracts\\TokenHandler::class,

App\\Services\\MysqlTokenHandler::class

);

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

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

?

1

2

3

4

5

6

7

8
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'));

}

}

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

?

1
$currentUser = app(\\App\\Contracts\\TokenHandler::class)->getTokenUser($request->input('api_token'));

以上这篇浅谈lumen的自定义依赖注入就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/zergling9999/p/7413006.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 浅谈lumen的自定义依赖注入 https://www.kuaiidc.com/94155.html

相关文章

猜你喜欢
发表评论
暂无评论