Snippets

Laravel API add Access-Control-Allow-Origin in the header response

Hi,

If you are using Laravel as backend and some other service for the frontend, where you want to add Access-Control-Allow-Origin in the API header response because of __ 'Cross-Origin Request Blocked' __, then this post might be helpful for you.

Of course, there are many ways to solve this issue. However, I discovered an easiest way to do that.

In your laravel project, go to bootstrap/app.php file and add the following lines before return-


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');

Done.

After restarting your service, you can try now to access via frontend consumers to access your API. Hope it will be working.

Thank you.