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 add a name prefix to all routes in a group?

Force trailing slash (redirect if no trailing slash exists) in .htaccess

How to set environmental variables in your .htaccess file

What is the null coalescing operator in PHP7?

How to output (echo/print) content in PHP

How to review blog posts (written by other users) before they get published on your WordPress blog?

How to set up an alias to map one directory to another destination

What are the SPL data structures

How to hide certain file types from directory listings

How do Invokable Controllers work in Laravel?