Yii::app()和>;之间有什么区别;用户->;setState()和Yii::$app->;请求->;饼干

Yii::app()和>;之间有什么区别;用户->;setState()和Yii::$app->;请求->;饼干,yii,yii2,Yii,Yii2,我的代码调用了Yii::app()->user->setState()。我想知道它将被保存在哪里,以及setState()和Yii2中的cookies之间的主要区别是什么?首先,Yii::app()->user->setState()来自Yii1,而Yii:$app->request->cookies来自Yii2 关于setState()的内容,在俄罗斯官方论坛上有很好的解释。其本质是用于保存经常使用的用户相关数据,并在以后访问这些数据。它同时使用会话和cookie,其主要优点是在不同会话之间保

我的代码调用了
Yii::app()->user->setState()
。我想知道它将被保存在哪里,以及
setState()
和Yii2中的cookies之间的主要区别是什么?

首先,
Yii::app()->user->setState()
来自Yii1,而
Yii:$app->request->cookies
来自Yii2

关于
setState()
的内容,在俄罗斯官方论坛上有很好的解释。其本质是用于保存经常使用的用户相关数据,并在以后访问这些数据。它同时使用会话和cookie,其主要优点是在不同会话之间保持一致


至于
Yii::$app->request->cookies
,官方文件中有相当详细的用法示例说明。

首先,
Yii::app()->>user->setState()
来自Yii1,而
Yii:$app->request->cookies
来自Yii2

关于
setState()
的内容,在俄罗斯官方论坛上有很好的解释。其本质是用于保存经常使用的用户相关数据,并在以后访问这些数据。它同时使用会话和cookie,其主要优点是在不同会话之间保持一致

至于
Yii::$app->request->cookies
,官方文档中提供了非常详细的使用示例说明。

setState()
用于在
会话中保存值,中的函数
setState()
可能会给您一个清晰的概念:

Yii::app()->user->setState('ccGiftItemDetail', $giftItemDetail );
Yii::app()->user->setState ('ccSelectedMerchantId', $model->attributes ['merchantId']);
顾名思义,
Yii::app()->request->cookies
处理的,看看函数
addCookie()
,它可能会给你一个想法:

    /**
     * Stores a variable in user session.
     *
     * This function is designed to be used by CWebUser descendant classes
     * who want to store additional user information in user session.
     * By storing a variable using this function, the variable may be retrieved
     * back later using {@link getState}. The variable will be persistent
     * across page requests during a user session.
     *
     * @param string $key variable name
     * @param mixed $value variable value
     * @param mixed $defaultValue default value. If $value===$defaultValue, the variable will be
     * removed from the session
     * @see getState
     */
    public function setState($key,$value,$defaultValue=null)
    {
        $key=$this->getStateKeyPrefix().$key;
        if($value===$defaultValue)
            unset($_SESSION[$key]);
        else
            $_SESSION[$key]=$value;
    }
setState()
用于在
会话中保存值
,中的函数
setState()
可能会让您清楚地了解:

Yii::app()->user->setState('ccGiftItemDetail', $giftItemDetail );
Yii::app()->user->setState ('ccSelectedMerchantId', $model->attributes ['merchantId']);
顾名思义,
Yii::app()->request->cookies
处理的,看看函数
addCookie()
,它可能会给你一个想法:

    /**
     * Stores a variable in user session.
     *
     * This function is designed to be used by CWebUser descendant classes
     * who want to store additional user information in user session.
     * By storing a variable using this function, the variable may be retrieved
     * back later using {@link getState}. The variable will be persistent
     * across page requests during a user session.
     *
     * @param string $key variable name
     * @param mixed $value variable value
     * @param mixed $defaultValue default value. If $value===$defaultValue, the variable will be
     * removed from the session
     * @see getState
     */
    public function setState($key,$value,$defaultValue=null)
    {
        $key=$this->getStateKeyPrefix().$key;
        if($value===$defaultValue)
            unset($_SESSION[$key]);
        else
            $_SESSION[$key]=$value;
    }

Yii::$app->session->set('someextradata','somevalue')
对于每个用户都是唯一的。

Yii::$app->session->set('someextradata','somevalue')
对于每个用户都是唯一的。

参考:参考: