Hello @tolgayigit ,
I'll suggest to use Middleware where you could check for ?ref_id=10 and make a cookie using the queue method, that way Laravel will set it for you with the next response.
Here are the steps to reproduce:
- go in terminal and type
php artisan make:middleware Referrerand Laravel will generate a class Referrer in app/Http/Middleware - in
handlemethod of that class type in the following:
public function handle($request, Closure $next)
{
if($request->has('ref_id')) {
Cookie::queue('ref_id', $request->get('ref_id'), 43200); // 43200 in minutes are 30 days
}
return $next($request);
}
-
register your middleware in app/Http/Kernel as route middleware, e.g. append
'referrer' => App\Http\Middleware\Referrer::classintoprotected $routeMiddleware -
assign 'referrer' to your "home" route in routes/web.php, for example
Route::get('/home', 'HomeController@index')->name('home')->middleware('referrer');
And whenever there is ref_id query parameter in your home route (http://www.example.com?ref_id=10) a cookie will be set on the client's browser.
Cheers