在组件中声明session 组件开启数据库库存储
[
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii',
'username' => 'root',
'password' => 'xxxxxx',
'charset' => 'utf8'
],
'session' => [
'class' => 'yii\web\DbSession', //session 操作对象
'db' => 'db', //指定数据库操作组件是上面的组件db
'sessionTable' => 'yii_session' //session 数据库表名称
]
]
]
yii_session 表结构
CREATE TABLE yii_session
(
id CHAR(40) NOT NULL PRIMARY KEY,
expire INTEGER,
data BLOB
)
namespace app\controllers\home;
use Yii;
use yii\web\Controller;
class IndexController
{
public function actionIndex()
{
$session = Yii::$app->session;
if(isset($session['name']) && isset($session['tel']))
{
var_dump($session);
}else{
$session['name'] = '谭勇';
$session['tel'] = 22;
echo 'set session';
}
}
}
测试 http://host/index.php?r=home/index/index