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 enable the rewrite engine in .htaccess?

How to exclude URLs from the Laravel CSRF protection?

How to block IP addresses in Apache by using regular expressions (regex) in .htaccess

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How to autoload helper files with composer?

How to check if a user is logged in, inside a blade file?

How to show (or log) all SQL queries executed by Laravel

How to check if a (blade) view file exists

How to return JSON data to the user in Laravel?

How to create random numbers in JS?