Php laravel framework quickly integrates with WeChat login

Source: Internet
Author: User
This article is intended for users of the php laravel framework. it introduces a simple integrated login method based on this framework, if you need it, refer to this article for users of the php laravel framework, and introduce a simple integrated login method based on this framework. The usage is as follows:

1. install php_weixin_provider

Run composer require thirdproviders/weixin in the project to complete the installation. After the installation is successful, you should be able to see the php_weixin_provider library file under the vendor Directory of the project:

2. configure login parameters

A total of seven parameters can be configured:

Client_id: application appid created for the public account

Client_secret: corresponds to the application appid created by the public account.

Redirect: The callback address after successful authorization

Proxy_url: the authorized proxy service address (for more information, see this article)

Device: The difference is PC or mobile. the default value is pc. if it is mobile, it can be set to null.

State_cookie_name: the authorization link contains a random state parameter. this parameter is returned when the callback is performed. you can verify whether the state parameter is the same as the parameter passed in the authorization link, to determine whether the request is valid and prevent CSRF attacks. In this scheme, the state parameter is first stored in the cookie during Authorization. Therefore, this parameter is used to specify the name of the cookie stored in the state parameter. the default value is wx_state_cookie.

State_cookie_time: specifies the validity period of the wx_state_cookie. the default value is 5 minutes.

These seven parameters can be set in two ways.

The first is to configure these parameters in the. env configuration file in an uppercase format:

Note: 1. each configuration item is in uppercase and starts with WEIXIN _. 2. the first three configuration items are inconsistent with the preceding parameter names. The KEY corresponds to client_id and SECRET corresponds to client_secret, REDIRECT_URI corresponds to redirect; 3. Others are the same as the preceding parameter names.

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

For configuration in this way, the name of each configuration item is consistent with that described earlier.

Note:

Because php_weixin_provider is implemented based on laravel/Societe, client_id, client_secret, and redirect must be configured. Otherwise, an error occurs during the php_weixin_provider instantiation process. for client_id and client_secret, I think unified configuration is fine in one place, but for redirect, if unified configuration does not necessarily meet the needs of all scenarios, because not all login locations are used, the last callback address is the same. Therefore, we recommend that you configure redirect as a valid or invalid non-empty callback address; you can change the value of php_weixin_provider when calling php_weixin_provider.

If proxy_url exists, it is recommended to set it in a public place;

Because both state_cookie_name and state_cookie_time have default values, there is basically no need to reconfigure them;
The device can be specified when it is used.

You can specify all configuration parameters when using them.

3. Register php_weixin_provider

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

4. register a third-party logon event listener

Add the following code to the app/Providers/EventServiceProvider. php of the project:

Laravel framework is a kind of IOC and event-driven thinking. when you are familiar with js, you will be very familiar with the event-driven idea. when you are familiar with the design mode, you will be familiar with IOC (Control reversal, also known as DI: dependency Injection) familiar. this is the key to understanding the role of step 2 and step 2 configuration.

5. Compile the logon interface

Example:

// Use a proxy to log on to the Route: get ('/login', function () {return Societe: with ('weixin ') -> setProxyUrl ('http: // proxy.your.com ')-> setRedirectUrl (url ('/login/Notify')-> redirect ();}); // use a proxy to log on to the Route: get ('/login2', function () {return Societe: with ('weixin ') -> setProxyUrl ('http: // proxy.your.com ')-> setDevice ('')-> setRedirectUrl (url ('/login/Notify ')) -> redirect () ;}); // do not use proxy jump. log on to the Route: get ('/login', function () {return Societe :: with ('weixin')-> setRedirectUrl (url ('/login/Notify')-> redirect () ;}); // do not use proxy jump, log on to the Route: get ('/login4', function () {return Societe: with ('weixin')-> setDevice ('') -> setRedirectUrl (url ('/login/logy')-> redirect ();});

Sociate:: with ('weixin') will return the instance of php_weixin_provider, that is, it:

After obtaining this instance, you can call all the public methods provided by the instance in a chained manner, such as setting configuration parameters and setDevice.

6. Compile the logon callback interface

Example:

// Logon callback Route: get ('/login/logs Y', function () {$ user = null; try {$ user = sociate::: with ('weixin ') -> user ();} catch (\ Exception $ e) {return 'get user exception';} return $ user-> nickname ;});

After the php_weixin_provider instance is obtained through sociate:: with ('weixin'), the user method is called automatically and the relevant interface is called, and the returned value is encapsulated into an object for returning. If any errors occur during this process, they will be thrown in the form of exceptions, such as state parameter verification failure, or code failure.

The returned $ user object contains the following valid attributes:

Summary:

This solution is implemented based on laravel/Societe and released to composer. Laravel/Societe is a third-party login module officially provided by laravel. Based on this module, it can easily integrate the authentication of most third-party platforms. Currently, it has provided many third-party login implementations: https://socialiteproviders.github.io /. In addition to facebook, google, github, and other foreign websites, domestic websites, Weibo, and qq are also provided. At the beginning, I used the default logon provider provided by the official website, but later I found the following problems:

1. authorization proxy not supported;

2. the pc end and the mobile end are actually implemented in two projects:

3. the encapsulated user object does not contain the unionid.

4. it is difficult to change the configuration parameters:

Therefore, based on its official logon provider, I implemented a new solution based on my own ideas to solve these problems I found.

For more articles about the php laravel framework's quick integration and logon method, refer to the PHP Chinese website!

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.