美文网首页
yii2 常用缓存与认证

yii2 常用缓存与认证

作者: songyu0 | 来源:发表于2017-05-14 19:00 被阅读64次

public function behaviors()

{

return [

// 访问过滤器

// 'verbs' => [

//    'class' => VerbFilter::className(),

//    'actions' => [

//        'delete' => ['POST'],

//    ],

// ],

// 缓存数据基本(变量)增删改查 --- 基本用法

// 设置重复键值的缓存时  不会被覆盖

// eg:$cache->set('key','H');

//      $cache->set('key','W');

//      echo $cache->get('key');

//      输出 H

//

// 获取缓存组件 基本

// $cache = \yii::$app->cache;

// 往缓存中写数据 增

// $cache->add('key','Hello!');

// 修改缓存数据 改

// $cache->set('key','H');

// 删除数据 删

// $cache->delete('key');

// 读取缓存 查

// echo $cache->get('key'); // 没有这个数据 返回 --- false

// 清空所有缓存

// $cache->flush();

// 缓存数据基本(变量) --- 基本用法 --- 生命周期定义 两种方式!!

// $cache->add('key','value','时间/秒');

// $cache->set('key','value','时间/秒');

/////////////////////////////////////////////////////////////////////////////

1111111111111111///////////////////文件绑定的缓存机制

$cache = \yii::$app->cache;

// 绑定文件

$dependency = new \yii\caching\FileDependency(['fileName' => 'hw.txt']);

// 如果绑定文件发生改变 (修改时间 ,内容)该值失效 返回false

222222222222/////////////////// 表达式的缓存机制

$dependency = new \yii\caching\ExpressionDependency(

//['expression' => 表达式]

['expression' => 'Yii::$app->request->get("name")']

);

// 如果绑定条件发生改变 该值失效 返回false

333333333333/////////////////// db绑定的缓存机制

$dependency = new \yii\caching\DbDependency(

//['sql' => '执行的sql语句']

['sql' => 'SELECT username FROM mly_user WHERE id=41']

);

// sql返回来的值发生变化后 缓存失效

//////////// 继上1 2 3  // 设置条件

$cache->add('file_key','value',3000,$dependency);

// 获取值

p($cache->get('file_key'));die;

/////////////////////////////////////////////////////////////////////////////

/////////////////// 纯页面缓存机制(该方式头部返回200信息)

// [

//    'class' => 'yii\filters\PageCache',

//    //缓存周期(秒)

//    'duration' => 3,

//    //绑定(一些条件)

//    'dependency' =>  [

//        'class'=> 'yii\caching\FileDependency',

//        // 文件绑定(文件变化)

//        'fileName' => 'hw.txt'

//    ]

// ]

////////////////////////////////////////////////////////

/////////////////////////////////片段缓存(多运用与页面当中)

// 缓存时间

// $duration = 秒速;

// 文件依赖

//$dependency = [

//  'class' => 'yii\caching\FileDependency',

//  'fileName' => 'hw.txt'

//]

// 缓存开关

$enabled = false;

// 如果有缓存 and 开始缓存

//beginCache('cache',['duration' => $duration(缓存时间)])): ?>

//beginCache('cache',['dependency' => $dependency(文件依赖)])): ?>

//beginCache('cache',['enabled' => $enabled(缓存开关[用不用缓存,是啥就是啥])])): ?>

//                                          ***缓存区域***

//                                          ***缓存区域***

//                                          ***缓存区域***

//  endCache(); 结束缓存

//  endif ?> 结束条件

////////////////////////////嵌套使用缓存 注意事项

////////////////////////////外部缓存时间 大于 内部时以 外部为主 内部没有机会被执行 只有外部过期内部才会被执行

////////////////////////////外部缓存时间 小于 内部时以 内部更新时间为主 只更新本身

// beginCache('cache',['duration'=>10])): ?>

//

//    beginCache('cache',['duration'=>1])): ?>

//

//    endCache();

//    endif ?>

// endCache();

//endif ?>

///////////////////////////////

/////////////    http 缓存(客户端)(一号例)  ---

/////////////    缓存的时间

/////////////    可对比信息头部返回信息 查看不同的值

[

'class' => 'yii\filters\HttpCache',

'lastModified' => function(){

return  123154(!缓存的时间!);

}

]

/////////////    http 缓存(客户端)(二号例)  ---

[

'class' => 'yii\filters\HttpCache',

'lastModified' => function(){//        !!时间!! 更新 内容

// 返回文件修改的时间

return filemtime('hw.txt');// 返回修改文件的时间

// return 1432817696; //通过 修改时间 判断 --

},

'etagSeed'=>function(){ //            !!缓存!! 更新 内容

$fp = fopen('hw.txt','r');// 打开文件--获取文件开启状态--准备操作

$title = fgets($fp); // 获取文件第一行的内容

fclose($fp);// 关闭文件

return $title;//返回信息--查看文件第一行有没有动过--动过更新缓存

// return 'etagSeed'; //通过 修改内容 判断 --

}

]

// 权限管理(传统)(一号例子)

'access' => [

'class' => AccessControl::className(),

'only' => ['index', 'update', 'delete','update'],

'rules' => [

[

'allow' => true,

'actions' => ['index', 'update', 'delete','update'],

'roles' => ['?'],

],

[

'allow' => true,

'actions' => ['index','update', 'delete','update'],

// @ 经过验证的用户 ? *-----* 未经验证的用户 游客!!

'roles' => ['@'],

// 允许访问的ip段

'ips' => ['127.0.0.1','::1'],

// 访问形式

'verbs' => ['POST','GET'],

//回调函数(条件满足)

// 'matchCallback' => function ($rule, $action) {

// return 之后 是 一个条件

// return date('y-m-d',time()) === '17-05-11';

// },

//回调函数(不加入权限控制之内) -------- !!!!'allow' => false,

// 'denyCallback' => function ($rule, $action) {

// return date('y-m-d',time()) === '17-05-12';

// echo '不赞成使用!';

// $this ->render('**');

// }

]

],

// 定制无权限 提示 (硬)!!

// 'denyCallback' => function ($rule,$action) {

//        throw new \Exception('You are not allowed to access this page');

// }

],

// 权限管理(传统)(二号例子)

'access' => [

'class' => AccessControl::className(),

'only' => ['index', 'update', 'delete','update'],

'rules' => [

[

'allow' => true,

'actions' => ['index','update', 'delete','update'],

// @ 经过验证的用户 ? *-----* 未经验证的用户 游客!!

'roles' => ['@'],

// 允许访问的ip段

'ips' => ['127.0.0.1','::1'],

// 访问形式

'verbs' => ['POST','GET'],

//回调函数(条件满足)

'matchCallback' => function ($rule, $action) {

// return 之后 是 一个条件

return date('y-m-d',time()) === '17-05-11';

},

//回调函数(不加入权限控制之内) -------- !!!!

'denyCallback' => function ($rule, $action) {

// 返回符合条件继续向下执行 否则 停止执行! 声明未开启 权限管理 'allow' => false,  rules上方【定义------》》》》》》】

// return date('y-m-d',time()) === '17-05-14';

// echo '不赞成使用!';

// $this ->render('**');

}

]

],

// 定制无权限 提示 (硬)!!

// 'denyCallback' => function ($rule,$action) {

//        throw new \Exception('You are not allowed to access this page');

// }

],

];

}

public function actionLogin()

{

// yii\web\User

// 类在登录和注销流程引发一些事件。

// yii\web\User::EVENT_BEFORE_LOGIN;

// 在登录 yii\web\User::login() 时引发。 如果事件句柄将事件对象的 yii\web\UserEvent::isValid 属性设为 false, 登录流程将会被取消。

// yii\web\User::EVENT_AFTER_LOGIN:

// 登录成功后引发。

// yii\web\User::EVENT_BEFORE_LOGOUT:

// 注销

// yii\web\User::logout()

// 前引发。 如果事件句柄将事件对象的 yii\web\UserEvent::isValid 属性设为 false, 注销流程将会被取消。

// yii\web\User::EVENT_AFTER_LOGOUT:

// 成功注销后引发。

// 你可以通过响应这些事件来实现一些类似登录统计、在线人数统计的功能。例如, 在登录后 yii\web\User::EVENT_AFTER_LOGIN 的处理程序,你可以将用户的登录时间和IP记录到 user 表中。

// $url = Yii::$app->request->getHostInfo(); // 获取当前url的域名

// // echo Yii::$app->request->getPathInfo(); // 获取当前除url域名外的后段

// // die;

// $uarray = explode('//', $url);

// $server = $uarray[1];

// $server = explode('.',$server);

// $servername = $server[0];

// p(Yii::$app->user->isGuest);die;

if (!Yii::$app->user->isGuest) {

return $this->goHome();

}

$model = new LoginForm();

// if(yii::$app->request->isPost){

//    $post = Yii::$app->request->post();

//    p($post);

//    die;

// }

// array(3) {

//  ["_csrf"]=>

//  string(56) "LW8zdjEtTWp0B2EfU0Q1BV0cVkRmfx0nGThKOn4YLANvLEsFXWAlAQ=="

//  ["LoginForm"]=>

//  array(4) {

//    ["username"]=>

//    string(5) "admin"

//    ["password"]=>

//    string(6) "111111"

//    ["verifyCode"]=>

//    string(4) "grgi"

//    ["rememberMe"]=>

//    string(1) "0"

//  }

//  ["login-button"]=>

//  string(0) ""

// }

// p($post);die;

// 检测 是否 满足 更改 并 验证 是否有数据 login

// if ($model->load(Yii::$app->request->post())) {

if ($model->load(Yii::$app->request->post()) && $model->login()) {

// if($model->login()){

// 前后对应 前方是修改的数据 后方是where条件,可多个

User::updateAll(['login_time'=>time(),'login_ip'=>Yii::$app->request->userIP],['id'=> [Yii::$app->user->identity->id]]);

// 返回原有页面

return $this->goBack();

// }else{

// echo 'out';

// }

} else {

return $this->render('login', [

'model' => $model,

]);

}

}

// 当前用户的身份实例。未认证用户则为 Null 。

$identity = Yii::$app->user->identity;

// 当前用户的ID。 未认证用户则为 Null 。

$id = Yii::$app->user->id;

// 判断当前用户是否是游客(未认证的)

$isGuest = Yii::$app->user->isGuest;

相关文章

网友评论

      本文标题:yii2 常用缓存与认证

      本文链接:https://www.haomeiwen.com/subject/gngqxxtx.html