Cookie没有设置在laravel中

时间:2016-03-02 07:36:49

标签: cookies laravel-5.2

在检查cookie是否存在时遇到困难,然后创建/更新cookie。这是为了检查用户是否在网站上选择了货币首选项,如果是,则将其保存到cookie和会话中。为此,我编写了以下帮助函数。

use Log;
use App\Currency;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Cookie;

function store_currency($prefCurrency = null)
{
    // If not passed, get from context
    if( empty($prefCurrency) ){
        if(request()->has('store_currency')){
            $prefCurrency = request('store_currency');
        } elseif (session()->has('store_currency')) {
            $prefCurrency = session('store_currency');
        } elseif (request()->hasCookie('store_currency')) {
            $prefCurrency = cookie('store_currency');
        } else {
            $prefCurrency = env('APP_DEFAULT_CURRENCY');
        }
    }

    // Uppercase it
    $prefCurrency = strtoupper($prefCurrency);

    // Is this an active currency?
    try{
        $c = Currency::findOrFail($prefCurrency);
    }catch(ModelNotFoundException $mnfe){
        $prefCurrency = env('APP_DEFAULT_CURRENCY');
    }

    // Update the context
    //dump('Currency set to: ' . $prefCurrency);
    Cookie::forever('store_currency', $prefCurrency);
    session()->put('store_currency', $prefCurrency);

    // prints null always :(
    //Log::debug('cookie: ' .  Cookie::get('store_currency') );

    return $prefCurrency;
}

以下是我如何从控制器方法调用此辅助函数。

public function order(Request $request, $currency = null)
{
    $currency = store_currency($currency);

    $plans = ServicePlan::popuplarPlansForHome('UNL', 5)->get();
    $selectedPlan = $request->plan_id ? : '';
    $right_now = Carbon::now();
    return view('checkout/order', compact('plans', 'selectedPlan', 'right_now'))
        ->withCookie(Cookie::forever('store_currency', $currency));
}

看起来我在这里遗漏了一些东西,请帮忙。没有错误,但cookie没有出现。

P.S并且路线声明如下:

Route::group(['middleware' => 'web'], function () {
    Route::get('checkout/order/{currency?}', 'CheckoutController@order')->name('newOrder');
});

此外,当我尝试从视图中转储cookie时,我所看到的是:

store_currency=deleted; expires=Tue, 03-Mar-2015 10:43:39 GMT; path=/; httponly

1 个答案:

答案 0 :(得分:1)

<强>解决

这是一种新的控制器方法,它就像一个魅力。早些时候,cookie没有附加到响应中。

import React, { Component, PropTypes } from 'react';

class Sizes extends Component {

  constructor(props) {
    super(props);
    this.state = {
      XXS: false,
      XS: false,
      S: false,
      SM: false,
      M: false,
      L: false,
      XL: false,
      XXL: false,
    };

    this.sizes = ['XXS', 'XS', 'S', 'SM', 'M', 'L', 'XL', 'XXL'];
  }

  toggleOnOff(size) {
    this.setState({
      [size]: !this.state.size
    });
  }

  render() {
    let XXS = this.state.XXS ? 'on' : '' ;
    XXS += ' filter-filterSize-XXS' ;

    let XS = this.state.XS ? 'on' : '' ;
    XS += ' filter-filterSize-XS' ;

    let S = this.state.S ? 'on' : '' ;
    S += ' filter-filterSize-S' ;

    let SM = this.state.SM ? 'on' : '' ;
    SM += ' filter-filterSize-SM' ;

    let M = this.state.M ? 'on' : '' ;
    M += ' filter-filterSize-M' ;

    let L = this.state.L ? 'on' : '' ;
    L += ' filter-filterSize-L' ;

    let XL = this.state.XL ? 'on' : '' ;
    XL += ' filter-filterSize-XL' ;

    let XXL = this.state.XXL ? 'on' : '' ;
    XXL += ' filter-filterSize-XXL' ;

    return (
        <div
          className='filter-filterSize-buttons'
        >
          {
            this.sizes.map((size) => {
              const toggleOnOff = this.toggleOnOff.bind(this, size);
              return (
                <a href="#" className={[size]} onClick={toggleOnOff}>{size}</a>
              )
            })
          }
        </div>
    );
  }
}

export default Sizes;