How to Submit a Non-trivial PR to a Laravel Package

If you want to contribute to a Laravel package with a PR, sometimes the Github interface is enough. You change one or two files there a...

Cristian Tabacitu
Cristian Tabacitu
Share:

If you want to contribute to a Laravel package with a PR, sometimes the Github interface is enough. You change one or two files there and submit your small PR. But more often than not, when your PR is more than a quick fix, you'll need to do this right. You'll need to test your work, before you submit the PR. To do that, you'll want to install the package on your computer, make the changes, test your work, and only then submit the changes as a PR.

This is how I do it. The process below works even if you can't normally install the package in your project with Composer, because of dependency conflicts.

Let's say you'd like to make profound changes to UserManager to provide support for Backpack v4, when previously it only supported Backpack v3.

  1. Fork the repo using Github's button.

  2. Clone your fork inside a Laravel installation:

git clone [email protected]:tabacitu/UserManager.git packages/usermanager
# notice we've cloned the package inside packages/packagename
  1. Add the service provider to your config/app.php file:
        EduardoArandaH\UserManager\UserManagerServiceProvider::class,
  1. Add the local requirement to your composer.json file:
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
-            "App\\": "app/"
+            "App\\": "app/",
+            "EduardoArandaH\\UserManager\\": "packages/usermanager/src"
        }
    },
  1. Run composer dump-autoload

This will make your Laravel installation run the package from your computer, using a git repo that is linked to your fork. When you're done making changes you can:

cd packages/usermanager
git commit -am "changes"
git push origin master
# this will have pushed your changes online, on your fork
# now you can go to your fork on Github, and use interface to start a new Pull Request to the original repo 
  1. Optional If the package is NOT a direct dependency, but a dependency of a dependency, you might need to also require it in your project's composer.json and alias it to a version that package needs:
    "require": {
        "eduardoarandah/usermanager": "dev-master as 2.0.99"
    },
  1. Optional If the package still doesn't get pulled from packages/, you can force Composer to do that, in your composer.json:
    "repositories": [
        {
            "type": "vcs",
            "url": "packages/usermanager"
        }
    ],

This could look more complicated than necessary, at first glance. And it is. But it's the fastest way I know how to do this, without using any third-party package or library. If you know a better way, let me know.

Hope it helps someone, someday.

Cristian Tabacitu
Cristian Tabacitu
Share:

Latest Articles

Wondering what our community has been up to?