Yii 简单的过滤器

2022-07-27 16:14:38 浏览数 (2)

代码语言:javascript复制
<?php
/**
 * Created by ZhengNiu.
 * User: admin
 * Date: 2018/01/6
 * Time: 14:20
 */

namespace appcontrollers;


use apphelpershelpers;
use appmodelsLoginForm;
use Yii;
use yiifiltersAccessControl;

class TestBehaviorsController extends BaseController
{
    public function actionIndex()
    {
        if (Yii::$app->user->isGuest) {
            echo '我是index';
        }else {
            echo Yii::$app->user->identity->username.'<a href="/test-behaviors/logout">退出</a>';
        }
        exit();
    }

    public function actionLogin()
    {
        $model = new LoginForm();
        if (Yii::$app->request->isPost) {
            $post['LoginForm']['username'] = Yii::$app->request->post('username');
            $post['LoginForm']['password'] = Yii::$app->request->post('password');
            $post['LoginForm']['rememberMe'] = Yii::$app->request->post('rememberMe');
            if ($model->load($post) && $model->login()) {
                return $this->redirect('/test-behaviors/index');
            }
        }
        return $this->render('login');
    }
    public function actionLogout()
    {
        Yii::$app->user->logout();
    }

    /** 登入后访问
     * @return string
     */
    public function actionMine()
    {
        return $this->render('mine');
    }

    /**
     *
     */
    public function actionInfo()
    {
        return $this->render('info');
    }

    /**
     * 行为
     * @return array
     */
    public function behaviors()
    {
        return [
            'acf' => [
                'class' => AccessControl::className(),
                'only' => ['mine', 'index','info'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['mine'],
                        'roles' => ['@'],//?未登入@登入
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index'],
                        'roles' => ['?', '@'],//?未登入@登入
                    ],
                    [
                        'allow' => true,
                        'actions' => ['info'],
                        'matchCallback' => function(){
                            //return true;可以访问
                            //return false;不可以访问
                            //活动页2018/1/6 15:30-15:38可以访问
                            if (strtotime('2018-01-06 15:30:00') <= time() &&
                                strtotime('2018-01-06 15:47:00') > time()) {
                                return true;
                            }
                            return false;
                        }
                    ],
                    //其他禁止
                ]
            ]
        ];
    }
}
代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="<?= yiihelpersUrl::to(['test-behaviors/login']) ?>">
    <div> 用户名:<input type="text" name="username"></div>
    <div> 密码:<input type="text" name="password"></div>
    <div> 记住我:<input type="checkbox" name="rememberMe" value="1" checked></div>
    <input type="hidden" name="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
    <div><input type="submit" name="提交"></div>
</form>
</body>
</html>
yii

0 人点赞