duanmie9682 2017-01-24 14:39 采纳率: 100%
浏览 493

Laravel在中间件中设置cookie,因此它可以在同一请求中使用

I have a Laravel middleware which sets a cookie if it doesn't exist already:

public function handle($request, Closure $next)
    {
        if (!$request->cookie('cart_id')) {
            return $next($request)->withCookie(cookie()->forever('cart_id', $this->generateUniqueCartId()));
        }

        return $next($request);
    }

Simple! The issue is if I try to access that cookie from a Controller on the same request, it returns null

$request->cookie('cart_id')

It's only on subsequent requests that the request contains the cookie.

I understand that the original request never contained the cookie so it makes sense that it returns null but I'm wondering if there's a clean way of accessing it or will I have to flash it into the session to access it later in that request?

  • 写回答

1条回答 默认 最新

  • dpjpo746884 2017-01-24 15:19
    关注

    I've solved my issue by creating a class that deals specifically with that cart Cookie generation and making it a singleton class that's injected into the service container.

    Once it generates a unique cart id, it will store it so I can access it later in the same request with app(CartCookie::class)->id(). If anyone can think of a better way that's still clean then by all means let me know.

    Here's the code:

    CartCookie.php

    namespace App\Services;
    
    use App\Cart;
    use Illuminate\Http\Request;
    
    class CartCookie
    {
        private $id;
        private $request;
    
        function __construct(Request $request)
        {
            $this->request = $request;
    
            if ($request->cookie('cart_id')) {
                $this->id = $request->cookie('cart_id');
            } else {
                $this->id = $this->generateUniqueCartId();
            }
        }
    
        public function id()
        {
            return $this->id;
        }
    
        private function generateUniqueCartId()
        {
            do {
                $id = md5(time() . 'cart' . rand(100000000000000, 9999999999999999));
            } while (Cart::find($id));
    
            return $id;
        }
    }
    

    CartCookieServiceProvider.php

    namespace App\Providers;
    
    use App\Services\CartCookie;
    use Illuminate\Http\Request;
    use Illuminate\Support\ServiceProvider;
    
    class CartCookieServiceProvider extends ServiceProvider
    {
        public function boot(Request $request)
        {
            $this->app->singleton(CartCookie::class, function ($app) use ($request) {
                return new CartCookie($request);
            });
        }
    }
    

    CartCookieMiddleware.php

    namespace App\Http\Middleware;
    
    use App\Cart;
    use Closure;
    
    class CartCookie
    {
        public function handle($request, Closure $next)
        {
            if (!$request->cookie('cart_id')) {
                $id = app(\App\Services\CartCookie::class)->id();
    
                return $next($request)->withCookie(cookie()->forever('cart_id', $id));
            }
    
            return $next($request);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了