Today I spent way too much time working with a custom Laravel package I’d made. The point was to be able to have this package repository hosted on Github, but have it be able to be installed via Composer into the typical /vendor folder with all of the other packages, and have it autoload. It took way too long as the documentation was pretty sparse, but like many things, the solutions was pretty simple, so I thought I’d share what I found to hopefully save someone else the trouble. This won’t delve into fully developing a Laravel package, just getting this to work with Composer and Github.

Autoloading the package

This isn’t really part of the github piece, but it was something I ran into anyways. To have the plugin autoload itself, simply have the following in the composer.json file for your package.

"autoload": {
    "psr-4": {
        "XFactor\\SelfMonitor\\": "src/"
    }
}

 

Getting Composer to Install From Github

Here’s the piece I was really struggling with, and is incredibly simple. This requires your server to be able to pull from the github repository if it’s a private repository. In the root composer.json file of your project, add a repositories entry like the one below, but add your repository url in place of the one below. After that, add the package within the required section of the composer.json file.

"repositories": [
    {
        "type": "vcs",
        "url": "git@github.com:yourname/yourpackage.git"
    }
],

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.2.*",
    "your/package": "1.0.0"
},

 

After that, you’ll just need to run composer update from the root of the project. One thing to note — when setting it up this way, you’ll need to change the version number in the require section if you need to update the package, composer won’t handle that for you.