Composer is awesome at package management. That's what it's for. Wouldn't it be nice though if you could just get it to make sure that a particular file or set of files are always loaded?

It's not unusual for a developer to want to bootstrap their application before dealing with a request, but leaving a trail of includes or requires hanging around isn't the best way to achieve this. Not when you've got Composer to hand.

The good news is that this is pretty simple and straightforward to achieve. First, let's consider the "Before" snapshot in some sample code.

<?php
require("/path/to/vendor/autoload.php");

// We'll get Composer to autoload these files
include_once("/path/to/config.php");
include_once("/path/to/bootstrap.php");

// ... 

I'm illustrating our example here with two not unusual use cases - pull in the config and then set up the application via a bootstrap file.

If your application has multiple entry points, these last two lines are the ones that you'd be requiring at each one. Got a front controller? That's one. Running unit tests? That's a second one.

But we know that copy and paste is bad. Code duplication is bad (although not very bad in this scenario).

Given that we're already using Composer, why not get it to load those files for us? That part is super easy.

First of all, we need to open composer.json in our preferred text editor. If your project doesn't have one, you can just go and create that file in your project's root directory.

Inside the file, we just need to locate the autoload section (or create it if it doesn't already exist).

Here's what a very basic composer.json file might look like if you were just using my PasswordManager library

{
    "require": {
        "vanqard/passman": "^2.0.2"
    },

    "autoload":  {
        "files": [
            "/path/to/config.php",
            "/path/to/bootstrap.php"
        ]
    }
}

As you can see, it's just the simple addition (or modification) of an autoload block to include the files key and an array definition inside square brackets of the files that need to be included.

If you only had the one file to include on every request, you could miss out the square brackets and just provide a single path to the "files" key. I chose two files to illustrate how this is achieved by defining a json array value.

The modification to our calling code is simple - delete the unnecessary include lines, like this.

<?php
require("/path/to/vendor/autoload.php");

// (include lines removed)

// rest of the code...

And our last task is to tell composer to rebuild its own autoloader files, so that our edits to composer.json get included.

php composer.phar dump-autoload   

That's all there is to it. Whenever you require("/path/to/vendor/autoload.php");, your config.php and bootstrap.php files are automagically included for you.

Thank you for reading. Your comments and feedback are most welcome below. If this was helpful to you, I would appreciate it if you shared a link to this page so that it might help others too.