My first PR in laravel/framework that got merged!

Amit Merchant · November 9, 2020 ·

Alright! So, my first PR in laravel/framework has got merged and I’m on cloud 9 right now. I’m not kidding!

After being in the community for a while, I have always wanted to contribute to Laravel’s codebase in some way but I have never got something worthwhile. But today, while going through the changelog of the recent Laravel release, I stumbled upon this PR which got my attention and inspired me to do my first contribution. I’m going to share my entire experience with this process. From forking the laravel/framework repo to get my PR merged!

The inspiration

I was looking into this PR and it’s about adding route regex registration methods whereNumber and whereAlpha which can be used to constrain the format of route parameters in a simple way.

So, if you want your route parameter to be always numeric, you can use whereNumber like so.

Route::get('authors/{author}')->whereNumber('author');

Similarly, the whereAlpha method can be used to constrain the route parameter to be always of alphabetic form.

Route::get('authors/{book}')->whereAlpha('book');

This is where the idea striked!

The idea

Looking at both of these methods, a potential method was missing according to me. And that method is the one that can be used to constrain the route parameter to be always of alphanumeric form.

I’ve decided to add this method. And decided to call it whereAlphaNumeric.

The implementation

I first forked the laravel/framework repository into my GitHub account and then cloned it into my machine.

Next, I created a feature branch called feature-alphanumeric-route-regex from 8.x.

The rest of the things were easy. I added the following method into src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php which is the meat of the functionality.

/**
* Specify that the given route parameters must be alphanumeric.
*
* @param  array|string  $parameters
* @return $this
*/
public function whereAlphaNumeric($parameters)
{
    return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+');
}

As you can see, under the hood, this method uses regex to ensure the given route parameters is alphanumeric.

I have also added relevant tests for the same in this file: tests/Routing/RouteRegistrarTest.php

public function testWhereAlphaNumericRegistration()
{
    $wheres = ['1a2b3c' => '[a-zA-Z0-9]+'];

    $this->router->get('/{foo}')->whereAlphaNumeric(['1a2b3c']);

    /** @var \Illuminate\Routing\Route $route */
    foreach ($this->router->getRoutes() as $route) {
        $this->assertEquals($wheres, $route->wheres);
    }
}

Once tests ran successfully, it was time to open the PR!

Raising the Pull Request

So, after ensuring nothing is breaking, I have raised this PR to be merged in the 8.x branch keeping finger crossed.

The PR got merged

It was after 2 hours after raising the PR, I got a mail that my PR has got merged! And you will not believe but it was like a dream true for me because not many get successful in getting their PR merged. But I was the lucky one. Thanks, Taylor!

And now I’ve become a small part of this monolithic framework! 😌

Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!