How to solve cross-domain problems in laravel development

不言
Release: 2023-03-31 22:36:02
Original
1660 people have browsed it

Recently, I encountered cross-domain needs during development, and found relevant solutions by searching for relevant information. Therefore, the following article mainly introduces to you the cross-domain solutions in laravel development. The article uses sample code The introduction is very detailed. Friends who need it can refer to it. Let’s take a look together.

Preface

As we all know, when we use laravel for development, especially when the front-end and back-end are completely separated, since the front-end project runs on The designated port of your own machine (it may also be someone else's machine), such as localhost:8000, and the laravel program runs on another port, so it is cross-domain. However, due to the browser's same-origin policy, cross-domain requests are illegal. of. In fact, this problem is easy to solve, just add a middleware. Not much to say below, let’s follow the editor to see the detailed solutions.

Solution:

1. Create a new middleware

php artisan make:middleware EnableCrossRequestMiddleware
Copy after login

2. Write the middleware content

<?php
namespace App\Http\Middleware;
use Closure;
class EnableCrossRequestMiddleware
{
 /**
 * Handle an incoming request.
 *
 * @param \Illuminate\Http\Request $request
 * @param \Closure $next
 * @return mixed
 */
 public function handle($request, Closure $next)
 {
 $response = $next($request);
 $origin = $request->server(&#39;HTTP_ORIGIN&#39;) ? $request->server(&#39;HTTP_ORIGIN&#39;) : &#39;&#39;;
 $allow_origin = [
  &#39;http://localhost:8000&#39;,
 ];
 if (in_array($origin, $allow_origin)) {
  $response->header(&#39;Access-Control-Allow-Origin&#39;, $origin);
  $response->header(&#39;Access-Control-Allow-Headers&#39;, &#39;Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN&#39;);
  $response->header(&#39;Access-Control-Expose-Headers&#39;, &#39;Authorization, authenticated&#39;);
  $response->header(&#39;Access-Control-Allow-Methods&#39;, &#39;GET, POST, PATCH, PUT, OPTIONS&#39;);
  $response->header(&#39;Access-Control-Allow-Credentials&#39;, &#39;true&#39;);
 }
 return $response;
 }
}
Copy after login

$allow_origin array variable is the list of cross-domain you allow and can be modified by yourself.

3. Then register the middleware in the kernel file

 protected $middleware = [
 // more
 App\Http\Middleware\EnableCrossRequestMiddleware::class,
 ];
Copy after login

$ in the App\Http\Kernel class The middleware attribute is added. The middleware registered here belongs to the global middleware.
Then you will find that the front-end page can already send cross-domain requests.

It is normal that there will be one more request with method set to options, because the browser must first determine whether the server allows the cross-domain request.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About Laravel receiving data analysis from front-end ajax

How to implement supervisor in Laravel framework in PHP Execute asynchronous process

The above is the detailed content of How to solve cross-domain problems in laravel development. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!