For example, I now have a token authentication system, now I use the MySQL token table implementation, in the future may be changed to Redis, how to achieve the future seamless connection.
Define a contract file first app/contracts/tokenhandler.php
<?phpnamespace app\contracts;/** * Handling Token contracts * @package app\contracts */interface tokenhandler{/** * Create a token * @param $userId integer User ID * @return string * /Public Function Createtoken ($userId) ; /** * User * @param $token String token value for the token * @return \app\user The user who owns the token */Public function Gettokenuser ($token); /** * Delete a token * @param $token String token value * @return BOOL succeeded */public function Removetoken ($token);}
There are 3 methods defined here: Create token, get token corresponding user, delete token.
Then we write a MySQL implementation under the app/services/mysqltokenhandler.php
<?phpnamespace app\services;use app\contracts\tokenhandler;use app\orm\token;/** * handling Token Contracts corresponding to MySQL Service * @package app\services */class Mysqltokenhandler implements tokenhandler{/** * @var int the maximum token a user can have */protected $userTokensMax = 10; /** * @inheritdoc * * Public Function Createtoken ($userId) {while (Token::where (' user_id ', $userId)-& Gt;count () >= $this->usertokensmax) {token::where (' user_id ', $userId)->orderby (' Updated_at ', ' ASC ')-& Gt;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 (); }}
Then bind the mappings between the two in the bootstrap/app.php:
$app->singleton ( app\contracts\tokenhandler::class, app\services\mysqltokenhandler::class);
If you change to Redis in the future, simply re-write a Redistokenhandler implementation and rebind, and the specific business logic code does not need any changes.
The object instance can then be injected directly into the controller, as long as the contract type is declared before the parameter:
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 ')); } }
You can also manually get an instance of the injected object in your code, such as:
$currentUser = App (\app\contracts\tokenhandler::class)->gettokenuser ($ Request->input (' Api_token '));