Last Updated: February 25, 2016
·
8.106K
· jason_au

Adding a custom GitHub repo to your Laravel 4 project using Composer

Composer is my favorite way to manage dependencies in PHP projects. Composer uses Packagist as its main repository but what if you need to add your own custom repository or package that is not in Packagist?

Recently, I've been using Composer a lot in a Laravel 4 project and I have just encountered the need to include a forked package. Luckily, Composer can work with any source control repository. In my case, I stored my forked package in GitHub.

To include your custom package, you can add the following snippets into your composer.json file:

{
    "repositories": {
        "sag": {
            "type": "package",
            "package": {
                "name": "jason-au/sag",
                "description": "Sag with fixed unitialized variables",
                "version": "0.8.0",
                "source": {
                    "url": "https://github.com/jason-au/sag",
                    "type": "git",
                    "reference": "23503d6bd35cb1bd39379ba968553f216120a1ec"
                }
            }
        }
    },
    "require": {
        "jason-au/sag": "0.8.*"
    },
    "autoload": {
        "psr-0": {
            "Sag": "vendor/jason-au/sag/src/"
        }
    }
}

The "repositories" block defines a list of your custom packages. In this case, I've defined a forked version of the Sag library.

Once defined, I can then include the library into my project under the "require" block. Note: the version matches up with what was defined in the "repositories" block.

Finally, the "autoload" section tells my application where the source is located. In this case, Laravel 4 source is placed under the vendor folder.