CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Laravel   Is Laravel Cors Not Working ? How to Fix access-control-allo ...

Is Laravel Cors Not Working ? How to Fix access-control-allow-origin Problem [Solution]

December 21, 2017 by SNK

laravel access control allow origin

Today in this laravel cors tutorial we are going to see how to fix access-control-allow-origin problem.

This generally occurs when you are going to send the data over the third party device like android or when working with cross platforms.

I faced this issue multiple number of times when working with vueJS and angular together with laravel as backend. So today we are going to see how to install cors and configure it to fix our problem of cors in our application.

Lets get started

What are we going to do ?

There are two ways of solving this issue :-

  1. One of them is by using the package from github repo by (barryvdh) and its awesome.
  2. Secondly,we are going to create our own cors and setup in your application (This is for them who dont want to use package).

 

Using Laravel Cors by Barryvdh

Package are way easy use and we are going to use laravel cors package by barryvdh. But sometimes beginners like me still get problem in configuring it even through package. So, here you will see the steps that you need to go through while configuring the package.

 

Fetch the package into your laravel app through commandline by using,

composer require barryvdh/laravel-cors

Once you finished installing it, its time to configure it. We need to make changes into config/app.php and app/Http/kernel.php files.

Go to config/app.php and paste this line,

Barryvdh\Cors\ServiceProvider::class, exactly as image below.

registering cors class in app service provider

Next step is to make changes in app/Http/kernel.php. Open it and add this \Barryvdh\Cors\HandleCors::class, line as in 3 sections inside kernel.php like in the image below.

Inside protected $middleware section.

protected middleware section

Next is to keep inside both web and api section inside protected $middlewareGroups section.

Inside Middleware Group

Lastly, in protected $routeMiddleware section we need to register the class'cors' => \Barryvdh\Cors\HandleCors::class,. This also helps to solve class cors does not exist issue in laravel.

Inside Route Middleware

Finally, go to back commandline again and type php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider".

It will publish the cors and you will find new file named cors.php in your laravel config folder where you can accept and allow the sites that you want to allow to use your apis.

Now if you try to send data over cross platform then, it will work now and will not show access-control-origin issue in your application.

Creating own laravel cors Middleware

Above, we looked towards how to use laravel cors package by barryvdh to solve access-control-origin problem in your app.

But now we will create our own cors class and its middleware to work with without having to use any third party package.

Create new file called Cors.php inside your laravel application app/Http/Middleware directory.

Cors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
 
namespace App\Http\Middleware;
 
use Closure;
 
class Cors {
    public function handle($request, Closure $next)
    {
     $domains = ['http://www.examplesite.com', 'http://www.examplesite2.com'];
        if(isset($request->server()['HTTP_ORIGIN'])){
            $origin = $request->server()['HTTP_ORIGIN'];
            if(in_array($origin, $domains)){
                header('Access-Control-Allow-Origin: '.  $origin);
                header('Access-Control-Allow-Headers: Origin, Content-Type');
            }
        }
        return $next($request);
            // ->header('Access-Control-Allow-Origin', '*')
            // ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

Now its time to register your newly made cors class into the app/Http/Kernel.php file so that we could use those in our routes.

Add  \App\Http\Middleware\Cors::class, inside your protected $middleware section like the image below inside kernel.php.

Adding Custom Cors

Finally, its done now you have to use our newly made cors middleware inside your routes. Open routes/api.php and write below line of code and use the middleware.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
use Illuminate\Http\Request;
 
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
 
Route::post('/cors-sample-test','ApiController@corsTest')->middleware('cors');
 
?>

 

Its done, you have finally made it. Beside from using package you need to explicitly define middleware in each single routes in your api.php, so i prefer you to use barryvdh cors package for it.

If you have any question or problems mention them in comments.

 

SHARE ON
Buffer

Enjoyed this article?

Like us on

Laravel class cors does not exist laravel 5.3 access-control-allow-origin laravel 5.5 cors laravel cors not working laravel cors tutorial

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.