Cookie :: get()返回null

时间:2017-07-16 22:49:17

标签: php laravel cookies laravel-5

我在获取Cookie时遇到问题,我在一个中间件中定义了一个cookie" CheckReferral",但是当我在一个Controller中调用cookie时,cookie返回null,我检查浏览器中的cookie和cookie在浏览器中很好,我不知道cookie的问题是什么......我用Google搜索了太多,这是我的最后一个资源,有人可以帮助我吗?

以下是中间件的代码:

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";

// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
    Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
};

// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
    ContentObject = new ContentObject (File.OpenRead (path), ContentEncoding.Default),
    ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
    ContentTransferEncoding = ContentEncoding.Base64,
    FileName = Path.GetFileName (path)
};

// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);

// now set the multipart/mixed as the message body
message.Body = multipart;

这就是我在控制器中调用cookie的方式:

<?php

namespace App\Http\Middleware;

use Closure;

class CheckReferral
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->hasCookie('referral')) {
            return $next($request);
        } else {
            if ($request->query('ref')) {
                return redirect($request->fullUrl())->withCookie(cookie()->forever('referral', $request->query('ref')));
            }
        }

        return $next($request);
    }

以下是存储在浏览器中的cookie:

Why I can't use this stored?

这是数据库......字段refer_by,存储为null,但应存储cookie的值:

Why the cookie return null?

非常感谢,我希望解决问题,并了解原因...

2 个答案:

答案 0 :(得分:1)

确保您已在app\Http\Kernel.php中定义了该中间件。特别是在'web'数组中。

web.php(路线)文件中导入中间件。

..最后为你的路线添加了这样的中间件:

Route::web('/', ['middleware' => CheckReferral']

如果您已经这样做,请确保您已将referred_by列添加到$fillable[]模型的User数组中。

编辑。 这样做:

\Request::cookie('referral');

答案 1 :(得分:0)

我使用vanilla PHP解决了它,全局变量$_COOKIE

protected function create(array $data)
{
    // $referred_by = User::where( 'affiliate_id', Cookie::get( 'referral' ) )->first();
    // $referred_user = Cookie::get( 'referral' );

    return User::create([
        'name'         => $data['name'],
        'email'        => $data['email'],
        'password'     => bcrypt($data['password']),
        'affiliate_id' => $this->uniqueRandomString(),
        'referred_by'  => $_COOKIE['referral'],
    ]);
}

Cookie::get('referral') $_COOKIE['referral']不是优雅解决方案,而是工作......