Make a custom Composer Package.

Categories: Development

First, what is Composer and what is a Package? Composer is a dependency manager for PHP. Those Dependencies are what we call Packages that don’t have to be but are usually stored on Packagist.

Making a private Composer Package is really simple, but the first thing that you need to make a new repo for it. Technically this can be a public repo that you don’t load through Packagist but preferably you will be creating a private repo. For work projects I use Github and personal projects, I like to use BitBucket because it’s free.

Here is a sample composer.json file to get you started. Make sure that you have all of your source code located in the folder /src.

{
  "name": "adam/core",
  "description": "Core services used by my Laravel sites.",
  "type": "library",
  "homepage": "https://github.com/adampatterson/app-core",
  "support": {
    "issues": "https://github.com/adampatterson/app-core/issues",
    "source": "https://github.com/adampatterson/app-core"
  },
  "autoload": {
    "psr-4": {
      "Adam\\Core\\": "src/"
    },
    "files": [
      "src/Helpers.php"
    ]
  },
  "authors": [
    {
      "name": "Adam Patterson",
      "email": "[email protected]"
    }
  ]
}

If you would like to future proof your package for Laravel 5.5 auto discovery then add the following.

"extra": {
  "laravel": {
    "providers": [
      "Adam\\Core\\AppServiceProvider"
    ]
  }
}

If you want to use your private package with Laravel then have a look at a previous post that I wrote here on Using a private Composer package with Laravel. Private packages allow me to continue working in a traditional manner without exposing internal code that I wish to keep private.

It also allows me to keep continuity between private projects without the need for manual migrations between the two.


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!