How to use a forked Laravel Package instead of the original

Paul Balan
2 min readAug 11, 2021
Photo by Wan San Yip on Unsplash

During the development of phpReel I bumped into a problem. Laravel packages are amazing on their own, but what if I want to slightly modify something or straight up add entirely new features?

It was rather difficult to find a comprehensive solution on how to achieve such a thing thus I am writing this article to help others.

I am going to be using laravel-modules by Nicolas Widart as an example.

  1. Fork the package that you want to modify and then create a new branch for your changes (you can optionally not create a new branch and just use the available branches but that is not good practice).
  2. Now go to the composer.json file of your project and look for the line where the package is required.
"require": {
...
"nwidart/laravel-modules": "8.2",
...
}

3. Replace “8.2” with “dev-yourBranchName”. If your newly created branch is called “new-amazing-feature” then your composer.json should look something like this:

"require": {
...
"nwidart/laravel-modules": "dev-new-amazing-feature",
...
}

Don’t include “dev-” in the name of your branch. “dev-” is used by Composer to figure out the fact that you want to use your fork of the package and not the original.

4. Now that we told Composer the fact that we want to use our branch, we must tell him where to find it. To do that, copy-paste the following code right above the “require” array from your composer.json file.

"repositories": {
{
"type": "vcs",
"url": "https://github.com/phpreel/laravel-modules"
}
}

Replace phpreel with your GitHub username and laravel-modules with the name of the forked package

5. The very last step is to run composer update in your terminal so it can fetch the package from your fork instead of the original repository. If you followed all the steps your composer.json should look something like this.

...
"repositories": {
{
"type": "vcs",
"url": "https://github.com/phpreel/laravel-modules"
}
},
"require": {
...
"nwidart/laravel-modules": "dev-new-amazing-feature",
...
}
...

These were all the steps required to use a fork instead of the original package. If you found this article useful, please share it. If you want to support me, you should check out phpReel, my open-source subscription-based video streaming application.

Official Composer documentation: https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository

--

--

Paul Balan

Creator of phpReel, a free, MIT open-source subscription-based video streaming application that lets you create your platform for distributing video content