Handle CORS Requests in Laravel 6 Application

0
()

Today, We want to share with you Handle CORS Requests in Laravel 6 Application.In this post we will show you Laravel 6 CORS Middleware Tutorial, hear for Handling CORS in a Laravel application we will give you demo and example for implement.In this post, we will learn about CORS Middleware for Laravel 5/6 with an example.

Handle CORS Requests in Laravel 6 Application

There are the Following The simple About Handle CORS Requests in Laravel 6 Application Full Information With Example and source code.

As I will cover this Post with live Working example to develop Handle CORS Requests with VueJS Client & Laravel 6 API, so the some major files and Directory structures for this example is following below.

Read Also:  Pagination in PHP and MySQLi Source Code

Laravel 6 CORS Middleware Features

Adds CORS “barryvdh/laravel-cors” (Cross-Origin Resource Sharing) headers support in your Laravel 6 Web application using middleware.

  • Handles CORS pre-flight OPTIONS requests
  • Adds CORS headers to your responses

Handling CORS in a Laravel application

web/routes.php

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization');

Step 1: Create Product API Route

routes/api.php

Route::get('/products_get', function (Request $request) {
    return response()->json(['']);
});

Step 2: Product API to Extra Third party server File

index.php

<!DOCTYPE html>
<html>
<head>
    <title>Simple PHP Laravel 6 CORS Middleware Tutorial - TamilRokers</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" crossorigin="anonymous"></script>
</head>
<body>
<h1>Get Products List</h1>
<script type="text/javascript">
  $.ajax({
    type: "GET",
    dataType: "json",
    url: 'http://localhost/api/products_get',
    success: function(response){
      console.log(response);
    }
  });
</script>
</body>
</html>

Error

Read Also:  Laravel 500 internal server error

“Access to XMLHttpRequest at ‘http://localhost/api/products_get’ from origin ‘null’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

Step 3: Install barryvdh/laravel-cors

Install to composer command

composer require barryvdh/laravel-cors

config/app.php

'providers' => [
	....
	Barryvdh\Cors\ServiceProvider::class,
],

app/Http/Kernel.php

.....
protected $middleware = [
    ...
    \Barryvdh\Cors\HandleCors::class,
];
.....

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Download

I hope you get an idea about CORS Middleware in Laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  PHP curl Http authentication header from Client

How useful was this post?

Click on a star to rate it!

Free Live Chat for Any Issue