Monday, March 20, 2017

Laravel giving CORS error just for one route/controller

Note: Edit at the bottom of this question

I am having a very strange problem and Im starting to pull my hair out on this one. I am using Angular and Laravel 5.4.

I am using Barryvdh\Cors and it is working perfectly. However for some reason I am getting the following error for just one controller and nothing else!

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.example.com/packing?data_type=prealert&shipment_ref=1. (Reason: missing token 'access-control-allow-headers' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).

This only happens with packing.

Here is my route (I have ommitted lots of other lines just for ease of reading):

    Auth::routes();

    Route::get('/', 'HomeController@index');

    Route::group([ 'middleware' => 'cors'], function()
    {

        // works
        Route::resource('prealert', 'PrealertController');

        // doesnt work
        Route::resource('packing', 'PackingController');

        // works
        Route::resource('shipping', 'ShippingController');

    });

I am comparing the two controllers below, as you can see they are the exact same (I think??).

Yet if I change

Route::resource('packing', 'PackingController');

to

Route::resource('packing', 'PrealertController');

It works fine.

I am assuming it is nothing from the AngularJS side because I have been able to narrow it down to simply that line, i.e. It works when I simply use another controller even if the code is the same.

Here is my PackingController (Http/Controllers/PackingController.php)

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\PublishPackingRequest;
use Mockery\CountValidator\Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use DB;
use Auth;


class PackingController extends Controller {

    public function index( ) {

        die('test');

    }

}

And my PrealertController (Http/Controllers/PrealertController.php)

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\PublishPrealertRequest;
use Mockery\CountValidator\Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Auth;
use DB;

class PrealertController extends Controller {


    public function index() {
        die('test');
    }

}

EDIT:

So it gets even stranger, I duplicated my PackingController.php to a file called AnotherController.php and changed my route line to the following...

Route::resource('packing', 'AnotherController');

I also changed the class name to AnotherController

And even that worked! Maybe I should just try deleting the file



via Adrian

Advertisement