Implementing and using a third-party PHP library

Drupal 8 uses Composer for package dependencies and autoloading classes based on PSR standards. This allows us to use any available PHP library much more easily than in previous versions of Drupal.

In this recipe, we will add the Stack/Cors library to add CORS support to Drupal 8. Stack/Cors is a stack middleware that adds support to the Access-Control-Allow-Origin header used in web applications. Without specification, AJAX requests across different domains may fail.

Tip

In order to test CORS, you will need to make a cross-domain asynchronous JavaScript request. The Access-Control-Allow-Origin header defines domains that are allowed to perform these requests.

Getting ready

You need to have Composer ...

Get Drupal 8: Enterprise Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.