How to set up your local projects to contribute to Laravel (or any PHP library) : a practical guide
Tags: laravel, git, github, php
Published: 2019-02-06 10:20:50

---
title:  How to set up your local projects to contribute to Laravel (or any PHP library) : a practical guide
published: true
description:  How to contribute to laravel
tags: Laravel, git, github, php
---

Let’s imagine this scenario: You are working on a Laravel application, and you found a bug either in the framework itself or in one of the third party libraries you are using. You edit the code inside your project, or maybe you installed a fresh copy of Laravel and library that has the issue, and you edited the code there. You saved the changes, but when you went to your terminal to commit the changes, GIT just ignored what you did. You start wondering “What’s happening here?”.

&nbsp;

<img class="alignnone size-full wp-image-184" src="http://youghourta.com/wp-content/uploads/2019/02/laravel-how-to-contribute.jpg" alt="" width="1500" height="750" />

The first solution that might pop up in your head could be to read the “How to contribute” of the framework or the library documentation, and in most cases, you’d find there the necessary steps to contribute to the framework, but I always felt that something was missing in the documentation. Let me explain what I mean by that:

Let’s take the example of Laravel here, if you decide to contribute to the framework, you’d need to clone the <a href="https://github.com/laravel/framework">laravel/framework</a> repository, push your changes to your own copy of the repository (on github), and create a PR, but what’s missing here is How would you test your changes (bug fix, new feature, …) in a local project, since if you introduce the changes in that project you just can’t commit them and create a PR for them easily, and if you change the code in the <code>laravel/framework</code> repository you just cloned you might end up doing a lot of copy/pasting between this repository and your project in order to test those changes.

Luckily there is a cleaner way to deal with all this. I’m not going to walk you through the usual steps of creating a PR, I assume since you are reading this article you are already familiar with that. I’ll focus on how you should set up your local environment to streamline this whole process and show you how you could fix bugs and develop new features in the framework/library and test them locally before you create the PR you are about to send.
<h1 id="prepare-your-test-project-and-your-repository">Prepare your test project and your repository</h1>
First, you’d need to fork the <code>laravel/framework</code> repository and then clone it locally (the usual steps when it comes to contributing to any open source project) and then create a test application where you are going to test those changes (I’m calling it playground)
<pre class=" language-bash"><code class="prism language-bash">➜ laravel <span class="token function">pwd</span>
/home/djug/laravel
➜ laravel <span class="token function">ls</span>
framework playground
</code></pre>
<h1 id="make-composer-load-the-changes-from-the-local-repository">Make Composer load the changes from the local repository</h1>
Now, with the most important part. We need to tell the <code>playground</code> application to take into consideration the changes we introduce in the <code>/home/djug/laravel/framework</code> repository

We can do this via composer, and according to its <a href="https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository">documentation</a> and I’m quoting here:
<blockquote>
<h4 id="loading-a-package-from-a-vcs-repository">Loading a package from a VCS repository</h4>
There are a few use cases for this. The most common one is maintaining
your own fork of a third party library. If you are using a certain
library for your project and you decide to change something in the
library, you will want your project to use the patched version. If the
library is on GitHub (this is the case most of the time), you can
simply fork it there and push your changes to your fork. After that,
you update the project’s composer.json. All you have to do is add your
fork as a repository and update the version constraint to point to
your custom branch.</blockquote>
So the first part of the <code>composer.json</code> file (on my <code>playground</code> project) should look like this:
<pre class=" language-json"><code class="prism language-json"><span class="token punctuation">{</span>
    <span class="token string">"name"</span><span class="token punctuation">:</span> <span class="token string">"laravel/laravel"</span><span class="token punctuation">,</span>
    <span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"project"</span><span class="token punctuation">,</span>
    <span class="token string">"description"</span><span class="token punctuation">:</span> <span class="token string">"The Laravel Framework."</span><span class="token punctuation">,</span>
    <span class="token string">"keywords"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token string">"framework"</span><span class="token punctuation">,</span>
    <span class="token string">"laravel"</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>

    <span class="token string">"license"</span><span class="token punctuation">:</span> <span class="token string">"MIT"</span><span class="token punctuation">,</span>
    <span class="token string">"repositories"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"vcs"</span><span class="token punctuation">,</span>
            <span class="token string">"url"</span><span class="token punctuation">:</span> <span class="token string">"/home/djug/laravel/framework/"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token string">"require"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">"php"</span><span class="token punctuation">:</span> <span class="token string">"^7.1.3"</span><span class="token punctuation">,</span>
        <span class="token string">"fideloper/proxy"</span><span class="token punctuation">:</span> <span class="token string">"^4.0"</span><span class="token punctuation">,</span>
        <span class="token string">"laravel/framework"</span><span class="token punctuation">:</span> <span class="token string">"master"</span><span class="token punctuation">,</span>
        <span class="token string">"laravel/tinker"</span><span class="token punctuation">:</span> <span class="token string">"^1.0"</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre>
Now each time I change something in the <code>/home/djug/laravel/framework/</code> repository and commit the change (you need to commit it) and if I execute <code>composer update</code> on my <code>playground</code> project it will get the changes.
<h1 id="push-the-changes-to-your-remote-repository-and-create-a-pr">3/ Push the changes to your remote repository and create a PR</h1>
Now that your playground repository can pull the changes from your <code>framework</code> repository, you could update add all the changes you want to your code and then test them on a “real” project.

After you finish testing, all that you need to do is to push the changes to your remote repository (your own “copy” of the <code>framework</code> repository on GitHub for instance) and then create the pull request.

&nbsp;

Photo by <a href="https://unsplash.com/photos/FlPc9_VocJ4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">John Schnobrich</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>