Laravel设置一个cookie然后重定向到家

时间:2017-03-19 16:53:34

标签: php laravel

我有一段代码应该设置一个cookie然后重定向到home,设置cookie部分似乎错过了。可能是什么问题?

Route::get( '/r/{ref}', function($ref){
    $response = new Illuminate\Http\Response();
    $cookie = Cookie::make('ref', $ref);
    return Redirect::to('home')->withCookie($cookie);
});

1 个答案:

答案 0 :(得分:0)

将$ cookie更改为[$ cookie]并将withCookie更改为withCookies。

旧代码:

Route::get( '/r/{ref}', function($ref){
    $response = new Illuminate\Http\Response();
    $cookie = Cookie::make('ref', $ref);
    return Redirect::to('home')->withCookie($cookie);
});

工作代码:

Route::get( '/r/{ref}', function($ref){
    $response = new Illuminate\Http\Response();
    $cookie = Cookie::make('ref', $ref);
    return Redirect::to('home')->withCookies([$cookie]);
});