DEV Community

Discussion on: Let's build a super simple referral system with Laravel

Collapse
 
clementn profile image
Clement

Very nice and very useful. Thank you.
I use it with a small change.

I moved the code to write the referrer into the session

    if ($request->has('ref')) {
        session(['referrer' => $request->query('ref')]);
    }
Enter fullscreen mode Exit fullscreen mode

from app/Http/Controllers/Auth/RegisterController.php to a custom made middleware app/Http/Middleware/CheckReferrer.php

That way, the ?ref=referrer can be used on any link on the website and not only on register?ref=referrer

P.S. The custom middleware can be created using the artisan command:
php artisan make:middleware CheckAge
then it has to be registered within app/Http/Kernel.php

    protected $middlewareGroups = [
        'web' => [
...........
            \App\Http\Middleware\CheckReferrer::class,
        ],
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jcarlosr profile image
Juan Ramos

Nice suggestion Clement. Although I think the session won't last much and it can be replaced if the user visits a page with a new ref parameter.
I need to keep the referrer value for at least 7 days and set the value only if it's not already there. For this particular case I think I will be using Cookies.

Collapse
 
simioluwatomi profile image
Simi Oluwatomi

This is great. I really didn't think about this use case as mine was only focused on registration