Laravel 5:由自定义中间件设置的cookie在Controller中无法读取

时间:2018-02-19 09:11:52

标签: cookies laravel-5 laravel-middleware laravel-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,
    ];
}

1 个答案:

答案 0 :(得分:1)

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

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

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