Home > PHP Framework > ThinkPHP > How thinkphp completes cross-domain requests

How thinkphp completes cross-domain requests

PHPz
Release: 2023-04-13 16:22:23
forward
3335 people have browsed it

1. Header class

In the thinkphp framework, you can use the Header class to set the response header to realize the function of cross-domain requests. The specific method is to add the following code to the controller method:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
Copy after login

Among them, the first line of code indicates that cross-domain requests from all sources are allowed, or it can be set to a specific source; the second line of code indicates that cross-domain requests are allowed Request header information carried by the request. After setting this up, you can implement basic cross-domain requests.

2. Third-party libraries

In addition to using the Header class to set up cross-domain requests, you can also use third-party libraries to simplify this process. The following takes the mainstream CORS (Cross-Origin Resource Sharing) library cors extension as an example to introduce how to use a third-party library to implement cross-domain requests.

  1. Install cors extension

Open the command line window and execute the following command:

composer require topthink/think-cors
Copy after login
  1. Configuration cors expansion

In the project's config folder, create a cors.php file and enter the following code:

<?php

return [
    // 允许的请求域名
    &#39;allow_origin&#39;      => [&#39;*&#39;],
    // 允许的请求头信息
    &#39;allow_headers&#39;     => &#39;Origin, X-Requested-With, Content-Type, Accept&#39;,
    // 允许的请求方法
    &#39;allow_methods&#39;     => &#39;GET, POST, PUT, DELETE, PATCH&#39;,
    // 是否允许发送cookie
    &#39;allow_credentials&#39; => true,
    // 跨域请求缓存时间
    &#39;max_age&#39;           => 3600,
];
Copy after login

Among them, $allow\_origin represents the allowed request Domain name, can be set to a specific domain name, or can be set to the wildcard "*"; $allow\_headers represents the allowed request header information, $allow\_methods represents the allowed request methods, $allow\_credentials represents whether sending cookies is allowed, $ max\_age represents the cross-domain request cache time.

  1. Modify the configuration file

In the config folder of the project, find the app.php file, the configuration is as follows:

return [
    // ...
    &#39;middleware&#39; => [
        // ...
        \think\middleware\Cors::class,
    ],
];
Copy after login
  1. Call cors extension

In the controller method that requires cross-domain requests, you can directly call the method in cors extension to realize the setting of cross-domain requests:

use think\facade\Cors;

public function index() {
    Cors::allowAllOrigin();
    return json([&#39;code&#39; => 200, &#39;msg&#39; => &#39;success&#39;]);
}
Copy after login

After setting this, cross-domain requests can be implemented.

The above is the detailed content of How thinkphp completes cross-domain requests. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template