fruitcake / laravel-cors

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
MIT License
6.27k stars 613 forks source link

Breaking change after upgrading from 2.0.3 to 2.0.4. Error: Call to undefined method class@anonymous::onRequestHandled() #530

Open juhasev opened 3 years ago

juhasev commented 3 years ago

I am getting this error after upgrading the package.

This is caused by HandleCors::class not resolving from the container. Seems like the package now has an issue if HandleCors::class is loaded in $middlewareGroups. Adding HandleCors::class to the $middleware array does not solve the issue either. We have 3 different middleware groups and we are only using HandleCors::class in one of them.

Error: Call to undefined method class@anonymous::onRequestHandled()

/var/www/html/vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php:42
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:392
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:237
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:118

Here is the HTTP kernel

  /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        CheckForMaintenanceMode::class,
        TrustProxies::class,
        ValidatePostSize::class,
        TrimStrings::class,
        ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [

        'web' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            HandleCors::class,
            StartSession::class,
            ShareErrorsFromSession::class,
            VerifyCsrfToken::class,
            SubstituteBindings::class,
            'trial' => CheckTrialExpiration::class,
        ],

        'landing' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            StartSession::class,
            VerifyCsrfToken::class,
            SubstituteBindings::class,
        ],

        'api' => [
            SubstituteBindings::class,
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            CheckClientCredentials::class
        ],
    ];
barryvdh commented 3 years ago

You haven't changed anything else? Not sure why the HandleCors class would be null if it's resolved from the container

juhasev commented 3 years ago

No other changes. Reverted back to the previous version 2.0.3 and everything works properly with the above config.

barryvdh commented 3 years ago

With Laravel 8? No Octane or anything?

I just did composer require laravel/laravel en moved the HandleCors from $middleware to the web part and don't get any errors.

Does it happen immediately? Or on CORS requests?

barryvdh commented 3 years ago

It's strange cause this is calling it:

                $this->app->make(HandleCors::class)->onRequestHandled($event);

But why would that be null. It registered in register()

juhasev commented 3 years ago

This is Laravel 8 standard without Octane. The error happened during testing.

barryvdh commented 3 years ago

What do you mean during testing? A unit test? How does it look?

juhasev commented 3 years ago

Yes during a unit (feature) controller test. Standard Laravel test setup. The test bootstrap loads the HTTP kernel normally, so I would assume this would also happen outside the tests.

AIx86 commented 3 years ago

I got the same error after upgrading to 2.0.4. from 2.0.3 on Laravel 8.44.0, no Octane. The issue occurs only during phpunit feature tests. Like so:

    /**
     * @test
     * @return void
     */
    public function verifyResponse(): void
    {
        $this->withoutMiddleware([HandleCors::class])
        $response = $this->call('GET', '/api/v1/some-route');
        $response->assertStatus(Response::HTTP_OK);
    }

Here is the call stack:

3) Tests\Feature\someTest::verifyResponse
Error: Call to undefined method class@anonymous::onRequestHandled()

/var/www/html/vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php:42
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:392
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:237
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:118
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:509
/var/www/html/tests/Feature/someTest.php:88

After reverting to 2.0.3 the issue is resolved.