Hello Laravel Lovers, some time in our project we have to create an api for getting access often we called as common controller method, for example we have to access category from anywhere in own project, but that is not a good practice, because that API will be access from any where(Out of your URL).
So Laravel provides us a way from where we can block the access for API.
Its a simple way to block other URL. Open config->cors.php and change ‘allowed_origins’ like below code.
<?php return [ /* |-------------------------------------------------------------------------- | Cross-Origin Resource Sharing (CORS) Configuration |-------------------------------------------------------------------------- | | Here you may configure your settings for cross-origin resource sharing | or "CORS". This determines what cross-origin operations may execute | in web browsers. You are free to adjust these settings as needed. | | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS | */ 'paths' => ['api/*', 'sanctum/csrf-cookie'], 'allowed_methods' => ['*'], // 'allowed_origins' => ['*'], 'allowed_origins' => ['https://readytocode.net/'], 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => false, ];
You can put multiple domain to access your api, There is also another method from which you can allow or disallow your api, By creating a middleware.
On your demand i wll do that also, if you want then comment below.
Thanks and Happy Coding.