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
Looks like your controller might be stuck in a namespace autoload caching? I would try the following to resolve it, start by
composer -o dump-autoload
Then do a route cache refresh
php artisan route:cache
And then finally check if the route actually exists by using
php artisan route:list
I would also check if you can see the CORS middle ware is being applied to the route as you are not using it as a global middleware. It should come up like the "oauth,api.company,api.user" under the middleware section in the picture below
If this doesn't fix the issue then I would check the controller's Camel casing is in proper order matching it from the Actions section is the route:list