Laravel 5:自定义中间件设置的 cookie 在控制器中不可读

Laravel 5: cookie set by custom Middleware not readable in Controller

我用 Laravel 5 创建了一个网站。为了跟踪其他网站引用的用户,我使用了以下中间件,它通过请求参数识别引用并设置 cookie。

<?php

namespace App\Http\Middleware;

use Closure;

class AffiliateTrackingMiddleware {

    public static $trackingCookieName = "refId";
    public static $trackingURLParameter = "refId";

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $response = $next($request);

        if ($request->has(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingURLParameter)) {

            return $response->withCookie(cookie()->forever(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingCookieName, $request->get(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingURLParameter)));
        }

        return $response;
    }
}

在我的控制器中,我想检索 cookie 的值。我为此实现了以下功能。对于没有可用 cookie 的情况,我还会检查是否设置了请求参数来识别引荐来源网址。

protected function getAffiliateId($request = null) {

    $affiliateCookieValue = Cookie::get(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingCookieName);

    if (!empty($affiliateCookieValue)) {
        return $affiliateCookieValue;
    } else {
        if ($request !== null) {
            if (!empty($request->get(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingURLParameter))) {
                $affiliateRequestValue = $request->get(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingURLParameter);
                return $affiliateRequestValue;
            } else {
                $affiliateCookieValue = $request->cookie(\App\Http\Middleware\AffiliateTrackingMiddleware::$trackingCookieName);
                return $affiliateCookieValue;
            }
        } else {
            return "";
        }
    }
}

现在奇怪的是,在我的控制器的构造函数中,我能够通过 cookie 检索正确的会员 ID。但是,当我从控制器的另一个函数中调用函数 getAffiliateId 时,cookie 值仍然为空。

为什么我无法从控制器中的任何函数检索在中间件中设置的 cookie 值?

在我的浏览器中我也可以看到 cookie。

非常感谢。


添加:Http/Kernel的代码。php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\AfterMiddleware::class,
        \App\Http\Middleware\AffiliateTrackingMiddleware::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            // \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

由于您想在 web 中间件内部使用您的中间件,请将您的中间件附加到 web 组中间件的 end 处,如下所示:

'web' => [
    ...
    \App\Http\Middleware\AffiliateTrackingMiddleware::class,
]

同时确保当请求到达您的中间件时您的 cookie 不为空