How to autoload helper files with composer?

October 26, 2018

You can tell composer's autoload function to automatically include certain PHP files on every load. This is useful if you want to have a file with helper functions.

Add a files array to composer.json, within the autoload section. For example, add the middle section from the code below:

{
  "autoload": {
  "classmap": [
      "database\/seeds",
      "database\/factories"
  ],

  // COPY THE BELOW LINES - start copying from
  // below this line (do not include this comment)
  "files": [
      "app\/Helpers\/MyHelpers.php",
      "ANY_OTHER_FILE_GOES_HERE_TOO"
  ],
  // UNTIL HERE - stop copying (and do not
  // include these comments in the .json!)

  "psr-4": {
      "App\": "app\/"
  }