skip to Main Content

I’m having trouble on some of my api calls. I was able to bypass this initial problem by using laravel cors by fruitcake

Response to preflight request doesn’t pass access control check

But the problems didn’t stop there, some api calls threw this error

"error":"Class cors does not exist","debug":{"code":-1,"line":767,"trace":[{"file":"/home/xxxxxx/Workspace/xxxxxx/vendor/laravel/framework/src/Illuminate/Container/Container.php","line":767…]}

I was able to follow the instruction from the fruitcake/laravel-cors including adding it to kernel.php, created config/cors.php with the paths set to 'paths' => ['api/*'], and finally adding this on my routes:

Route::group(['middleware' => 'cors'], function() {
    Route::middleware('auth:api')->namespace('Api')->group(function () {

     
        Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
            Route::get('dashboard/statistics', 'DashboardController@index');

...

I’m using an older version of Laravel (5.6) and PHP 7.4. Help would be much appreciated!

When I opened the endpoint on a new tab, an error appears:

Symfony Component HttpKernel Exception MethodNotAllowedHttpException No message

3

Answers


  1. Chosen as BEST ANSWER

    I removed the middleware on my router

    Route::group(['middleware' => 'cors'], function() {
        Route::middleware('auth:api')->namespace('Api')->group(function () {
    
     
            Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
                Route::get('dashboard/statistics', 'DashboardController@index');
    
    ...
    

    to just

    Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
        Route::get('dashboard/statistics', 'DashboardController@index');
    

  2. Add HandleCors class into config/app.php alias section like below.

    'cors' => FruitcakeCorsHandleCors::class,
    
    Login or Signup to reply.
  3. Did you run the composer dump-autoload command after adding HandleCors to the config/app.php ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search