Examples of lumen custom dependency injection in PHP

Source: Internet
Author: User
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 ')); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.