项目中的一些知识点

一、数据模型的创建样例

'管理员账号不能为空'],
            ['adminpass','required','message'=>'管理员密码不能为空'],
            ['rememberMe','boolean'],
            ['adminpass','validatePass'],
        ];
    }

    public function validatePass(){
        if (!$this->hasErrors()) {
            $data = self::find()->where('adminuser = :user and adminpass =:pass', [':user'=>$this->adminuser,":pass"=>md5($this->adminpass)])->one();
            if (is_null($data)) {
                $this->addError("adminpass",'用户名或者密码错误');
                return false;
            }
        }
    }

    public function login($data){
        if($this->load($data) && $this->validate()){
            $lifetime = $this->rememberMe ? 24 * 3600 : 0;
            $session = Yii::$app->session;
            session_set_cookie_params($lifetime);
            $session['admin'] = ['adminuser'=>$this->adminuser,'isLogin'=>1];
            $this->updateAll(['logintime'=>time(),'loginip'=>ip2long(Yii::$app->request->userIp)],'adminuser = :user',[':user'=>$this->adminuser]);
            return (bool)$session['admin']['isLogin'];
        }
        return false;
    }
}


?>

二、http请求方式的判断

    function actionLogin()
    {
        $model = new Admin;
        $this->layout = false;
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->login($post)) {
                $this->redirect(['default/index']);
                Yii::$app->end();
            }
        }
        return $this->render('login',['model'=>$model]);
    }

三、模板中链接的书写

四、session的移除

    function actionLogout(){
        Yii::$app->session->removeAll();
        if (!isset(Yii::$app->session['admin']['isLogin'])) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        $this->goback();
    }

四、模板中ActiveForm的使用


            ['template'=>'{error}{input}']]); ?>
                
慕课商城 - 后台管理

field($model,'adminuser')->textInput(['class'=>'span12','placeholder'=>'管理员账号']); ?>

field($model,'adminpass')->passwordInput(['class'=>'span12','placeholder'=>'管理员密码']); ?>
忘记密码?
field($model,'rememberMe')->checkbox(['id'=>'remember-me','template'=>'
{input}
']); ?>
'btn-glow primary login']); ?>

你可能感兴趣的:(项目中的一些知识点)