Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EventFellows's avatar

Possible to handle a cookie during registration of a new user in Spark?

I am in the process of building an existing-user-refers-a-new-user system where users are rewarded with credits if they refer new users.

  • a user has a unique rerferral key that is used to identify the referring user (e.g. John Doe with UUID 123-4567-abc)
  • when a referred user (=new user) uses the referral link (e.g. mysite.com/referral/123-4567-abc) a cookie is set with a validity of 1 month (this part works)
  • now, on each $request object of the new user the $request->cookie('referringUserId') is available during validity of cookie (also works)

When the new user registers I need to handle the rewards for both users. In Spark there is a UserRegistered event but that does not contain the $request->cookie() but only the the $user object and I do not want to change vendor code.

Where can I best handle the cookie?

Registration in Spark is handled here: vendor\laravel\spark\src\Http\Controllers\Auth\RegisterController.php in register-method.

0 likes
2 replies
EventFellows's avatar
EventFellows
OP
Best Answer
Level 16

Ok, I now figured it out.

Here are some general insights into how the swap methods work that I found quite useful:

The trick ended up being as follows:

  • do not try to intercept the RegisterController.php 's register-method but follow the lifecycle of the request up to the CreateUser handle-method (which also contains the $request) Then you can do something like this:
        Spark::swap('CreateUser@handle', function ($request) {

            \Clockwork::info($request->cookie('referringUserId'));

            return Spark::interact(UserRepository::class.'@create', [$request->all()]);
        });

I applied it within the SparkServiceProvider.php

Please or to participate in this conversation.