Home >Backend Development >PHP Tutorial >A brief discussion on custom dependency injection of lumen framework

A brief discussion on custom dependency injection of lumen framework

*文
*文Original
2018-01-03 16:09:141855browse

This article mainly talks about custom dependency injection of lumen framework. The editor thinks it’s pretty good, so I’d like to share it with you today for your reference. Let’s follow the editor to have a look, I hope it will be helpful to everyone.

For example, I now have a token authentication system. Currently I use mysql token table to implement it. It may be changed to redis in the future. How can I achieve seamless connections in the future?

First define a contract file app/Contracts/TokenHandler.php

<?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); 
}

There are 3 methods defined here: create token and get the token corresponding User, delete token.

Then we write an implementation app/Services/MysqlTokenHandler.php under Mysql

<?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(&#39;user_id&#39;, $userId)->count() >= $this->userTokensMax) { 
   Token::where(&#39;user_id&#39;, $userId)->orderBy(&#39;updated_at&#39;, &#39;asc&#39;)->first()->delete(); 
  } 
 
  $token = \Illuminate\Support\Str::random(32); 
  if (!Token::create([&#39;token&#39; => $token, &#39;user_id&#39; => $userId])) { 
   return false; 
  } 
 
  return $token; 
 } 
 
 /** 
  * @inheritdoc 
  */
 public function getTokenUser($token) 
 { 
  $tokenObject = Token::where(&#39;token&#39;, $token)->first(); 
 
  return $tokenObject && $tokenObject->user ? $tokenObject->user : false; 
 } 
 
 /** 
  * @inheritdoc 
  */
 public function removeToken($token) 
 { 
  return Token::find($token)->delete(); 
 } 
}

Then bind the mapping relationship between the two in bootstrap/app.php:

$app->singleton(
 App\Contracts\TokenHandler::class,
 App\Services\MysqlTokenHandler::class
);

If you switch to redis in the future, you only need to rewrite the implementation of RedisTokenHandler and rebind it. The specific business logic code does not need to be changed.

So you can directly inject the object instance in the controller, as long as you declare the contract type before the parameters:

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

You can also manually get the instance of the injected object in the code, such as:

$currentUser = app(\App\Contracts\TokenHandler::class)->getTokenUser($request->input(&#39;api_token&#39;));

Related recommendations:

Using Queue in Laravel

laravel writes APP interface (API)

Explore how Laravel’s middleware is implemented

The above is the detailed content of A brief discussion on custom dependency injection of lumen framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn