Handle CORS Requests in Laravel 6 Application

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.

Read Also:  PHP curl Http authentication header from Client

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.

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:  registration and signin form in php and mysql with validation

“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.

Read Also:  How to Set Headers in Laravel 6 Example?

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Add www subdomain to all Urls using Htaccess
  2. Read Also:  How to Change the src of an Image using jquery?
  3. Read Also:  Jquery Live Search Filter Example with Demo
  4. Read Also:  PHP Conditional Statements Example
  5. Read Also:  PHP Create PDF file using mpdf | tcpdf | fpdf | php html to pdf
  6. Read Also:  Laravel 6 Allow CORS All requests Middleware Tutorial
  7. Read Also:  How to Show or hide invisible characters?
  8. Read Also:  Sudo Unable To Resolve Host Error in Linux - Fix sudo
  9. Read Also:  how to pass array from view to controller?
  10. Read Also:  How to Show or hide invisible characters?
CLOSEX