Search code examples
laravelcsrf-protection

Laravel: Excluding routes from VerifyCsrfToken middleware programmatically


I would like to know if there's a way I can programmatically add route to $except variable in VerifyCsrfToken class.

class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            //
        ];
    }

I am developing a package that listens to webhooks/postback from a payment gateway. It would be nicer if my custom route would be added to $except variable upon installation of the package.

Thanks


Solution

  • I decided to add my custom route to api.php

    file_put_contents(
     base_path('routes/api.php'),
     file_get_contents(__DIR__.'/stubs/make/routes.stub'),
     FILE_APPEND
    );
    

    VerifyCsrfToken middleware does not intercept routes in api.php