尝试在中间件中取消设置 Laravel cookies

问题描述 投票:0回答:1

我尝试取消设置 Laravel cookies,我从堆栈中获取了多次帮助,但没有成功。

这是我的中间件。

    public function handle($request, Closure $next)
{
$response = $next($request);
if (!$request->query('ref') ) {
  // Add a cookie to the response that lasts 5 years (in minutes)
   Cookie::queue(Cookie::forget('referral'));
  //$response->cookie( 'referral', encrypt( $request->query('ref') ), 525600 );
}
else {
            if( $request->query('ref') ) {
                return redirect($request->fullUrl())->withCookie(cookie()->forever('referral', $request->query('ref')));
            }
        }
return $response;
}
php laravel
1个回答
0
投票

请检查此代码

use Illuminate\Support\Facades\Cookie;

public function handle($request, Closure $next)
{
    // Check if 'ref' query parameter exists
    if ($request->has('ref')) {
        // Redirect with the 'referral' cookie set to last forever
        return redirect($request->fullUrl())
            ->withCookie(cookie()->forever('referral', $request->query('ref')));
    }

    // If 'ref' is not present, forget the 'referral' cookie
    Cookie::queue(Cookie::forget('referral'));

    return $next($request);
}
© www.soinside.com 2019 - 2024. All rights reserved.