← Other topics

Composer require a forked repository

Video Notes

In this guide I’ll cover how to use a forked version of a Composer package on Github. This can come in handy as a temporary solution if you identify a bug or issue in a package you’re using and the package maintainer is slow to address the issue.

For this demo, I’ll work with a freshly installed Laravel application, but this would work with any Composer managed PHP project. Within the application, I’ll target the dependency guzzlehttp/guzzle as an example since it comes installed by default with Laravel applications.

Make the fork

To get started, first create a fork of the package you’re working with by visiting the package homepage on Github and clicking the Fork button on the top right.

Next, edit the forked repository to address whatever change is needed and commit those changes.

Use the fork

To get your application to use the forked version of the package, edit its Composer config file (composer.json) adding a repositories section with the following settings, replacing the value for url with the Github URL to your forked package:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/susanBuck/guzzle"
        }
    ],
],

Finally, update your require section, setting the version constraint of the package in question to be dev-branch where branch is whatever branch name you made your edits on in your forked repository.

In my example, I worked on the branch master so I’ll set the guzzlehttp/guzzle package to version dev-master:

"require": {
    "guzzlehttp/guzzle": "dev-master",
    [...]
},

The key thing to note here is your require section is still referencing the original package guzzlehttp/guzzle, but the addition of the repositories config and the update to the version constraint makes it so that it’s imported from your fork, not the original source.

Test it

To test things worked as expected, run composer update to update your dependencies and then find the underlying source code for the package in your Composer vendor directory. Examining the code files you should be able to see the changes you made within your forked repository.

Misc

Unlock all the notes for $4

No subscriptions, no auto-renewals.

Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.

Unlocking gets you access to the notes for this video plus all 200+ guides on this site.

Your support is appreciated. Thank you!

Payment Info

/
$4 6 months
$25 forever
Please check the form for errors
Questions? help@codewithsusan.com
← Other topics