Php laravel framework quickly integrates the WeChat login method. laravel framework

Source: Internet
Author: User

Php laravel framework quickly integrates the login method. laravel framework

This article is intended for users of the php laravel framework and introduces 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.

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.