PHP laravel Framework for fast integration of micro-letter Login Method _php Example

Source: Internet
Author: User
Tags php language

This paper is aimed at the user of PHP language Laravel framework, and introduces a simple integrated micro-letter login method based on the framework. Use the following methods:

1. Install Php_weixin_provider

The installation can be completed by running composer require thirdproviders/weixin under the project. After the installation is successful, you should be able to see the Php_weixin_provider library files in the project's Vendor directory:

2. Configure the parameters of the micro-letter Login

A total of 7 parameters can be configured, respectively:

    • CLIENT_ID: Application AppID for public number creation
    • Client_secret: Application AppID for public number creation
    • Redirect: The callback address corresponding to the success of the micro-letter authorization
    • Proxy_url: Corresponding to the micro-letter authorization of the Proxy service address (its role can read this article understand)
    • Device: The difference is PC-side micro-mail or mobile end of the micro-mail login, the default value is PC, if the mobile end, can be set to NULL
    • State_cookie_name: The authorization link will contain the random state parameter, which will be returned intact when the micro-mail callback, by verifying that the state parameter is the same as the parameter passed in the authorization link to determine if the request is valid and prevent CSRF attacks. This scheme will store the state parameter in the cookie at the time of authorization, so this parameter is used to specify the cookie name that the state parameter is stored in, and the default value is Wx_state_cookie
    • State_cookie_time: Specifies the effective time length of the Wx_state_cookie, which defaults to 5 minutes
    • These seven parameters are set in 2 ways.

The first is to configure these parameters in uppercase form into the. Env configuration file:

Note: 1, each configuration item is uppercase, and starts with Weixin_; 2, the first three configuration items are not exactly the same as the parameter names described earlier, and the key corresponds to the client_secret,redirect_uri corresponding to the Client_id,secret REDIRECT 3, the other is consistent with the name of the parameter described earlier.

The second is to configure these parameters into the config/services.php file:

In this manner of configuration, the name of each configuration item is consistent with the one described earlier.

What to note:

Because Php_weixin_provider is implemented based on Laravel/socialite, it requires client_id,client_secret and redirect to be configured, otherwise php_weixin_ The provider instantiation process goes awry; for client_id and Client_secret, I think the unified configuration is fine in one place, but for redirect, if the uniform configuration does not necessarily meet the needs of all scenarios, Because not every place where the micro-mail is used, the final callback address is the same; it is recommended that the redirect be configured as a valid or invalid Non-empty callback address, and that the value of this parameter can be changed at the time of the call using the Php_weixin_provider.

Proxy_url, if any, is also recommended to be configured in a public place;

State_cookie_name and State_cookie_time have a default value that basically requires no reconfiguration;
Device can be specified at the time of use.

All configuration parameters can be redefined when they are in use.

3. Register Php_weixin_provider

In the project's config/app.php file, find the Providers configuration section and add the following code to its configuration array:

4. Registration of third party login event monitoring

Add the following code to the app/providers/eventserviceprovider.php of the project:

Laravel framework as a whole is a kind of IOC and event-driven thinking, familiar with JS will be very familiar with event-driven, familiar with design patterns, will be the IOC (control inversion, also known as DI: Dependency injection) More familiar, this is to understand the 3rd and 4th step configuration role of the key.

5. Write the interface of micro-letter Login

Examples are as follows:

Using proxy jump, from PC-side micro-letter login
route::get ('/login ', function () {return
 socialite::with (' Weixin ')
  ->setproxyurl (' http://proxy.your.com ')
  ->setredirecturl (URL ('/login/notify '))
  ->redirect ();
});
Use the proxy jump, from the handset side micro-letter login
route::get ('/login2 ', function () {return
 socialite::with (' Weixin ')
  -> Setproxyurl (' http://proxy.your.com ')
  ->setdevice (')
  ->setredirecturl (url ('/login/notify '))
  ->redirect ();
});
Do not use proxy jump, from PC-side micro-letter login
route::get ('/login ', function () {return
 socialite::with (' Weixin ')
  -> Setredirecturl (URL ('/login/notify '))
  ->redirect ();
});
Do not use the proxy jump, from the phone-side micro-letter login
route::get ('/login4 ', function () {return
 socialite::with (' Weixin ')
  -> Setdevice (')
  ->setredirecturl (url ('/login/notify ')
  ->redirect ();
});

Socialite::with (' Weixin ') returns an instance of Php_weixin_provider, which is it:

Once you've got this instance, you can call all the public methods it provides, such as setting configuration parameters, Setdevice, and so on, in a chained way.

6. Write the interface of the micro-letter Login Callback

Examples are as follows:

Login callback
route::get ('/login/notify ', function () {
 $user = null;
 try {
  $user = socialite::with (' Weixin ')->user ();
 } catch (\exception $e) {return
  ' gets a micro-user exception ';
 }
 return $user->nickname;
});

by Socialite::with (' Weixin ') to get the Php_weixin_provider instance, the user method is invoked, the interface is automatically invoked with the micro-letter, and the return value of the micro-letter is encapsulated into an object return. If there is any error in this process, it will be thrown in the form of an exception, such as a state parameter checksum failure, such as code failure.

The $user object that is returned contains valid properties:

Summary:

This scheme is based on the Laravel/socialite implementation and is published to composer for use. Laravel/socialite is the official laravel of the third party login module, based on which it can easily integrate the majority of Third-party platform certification, currently it has provided a lot of Third-party login implementation: https://socialiteproviders.github.io/. In addition to foreign facebook,google,github and so on, the domestic micro-letter, micro-BO, QQ are also available. I also used the default micro-letter login Provider It officially provided, but then I found the following questions:

1. Agents that do not support micro-trust authorization;

2. PC end to mobile end is still divided into two projects to do:

3. It encapsulates a user object that does not contain Unionid

4. The way to change the configuration parameters is really difficult for people to use:

So I was in the official micro-letter login provider, based on their own ideas, to achieve a new solution to the problems I found.

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.