How to autoload helper files with composer?

Composer: How to autoload helper files with composer?

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:

  1.           "autoload": {
  2.         "classmap": [
  3.             "database\/seeds",
  4.             "database\/factories"
  5.         ],
  6.  
  7.         //// COPY THE BELOW LINES - start copying from below this line (do not include this comment)
  8.  
  9.         "files": [
  10.             "app\/Helpers\/MyHelpers.php",
  11.             "ANY_OTHER_FILE_GOES_HERE_TOO"
  12.         ],
  13.  
  14.         //// UNTIL HERE - stop copying (and do not include this comment)
  15.  
  16.         "psr-4": {
  17.             "App\": "app\/"
  18.        }
  19.    },

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to autoload helper files with composer?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Composer language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to destroy or remove a cookie in PHP

What are macros in Laravel?

How to manually write your own pagination in PHP

How to get the first row that matches some where queries, or create it if it doesn't exist (in Laravel's Eloquent)?

What are the predefined variables in PHP?

How to find the intersection (same values) from two arrays?

How to block multiple IP addresses in .htaccess

How to make multiple routes share the same URL structure or URL parameters

PHP Date commonly used formats

How to pick what array_intersect function to use in PHP?