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

Push vs Pop vs Shift vs Unshift

How to set environmental variables in your .htaccess file

How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

How to replace values in a string, in JS

What are PHP's PSRs?

How to select what columns to return when calling ::all()

How to set infinite execution time in a PHP script

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

What are the main error types in PHP

How to make Apache force download of files (in .htaccess)