我使用 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,
];
}