项目中的一些知识点

2017-03-11  本文已影响38人  liamu

一、数据模型的创建样例

<?php  
namespace app\modules\models;
use yii\db\ActiveRecord;
use Yii;

class Admin extends ActiveRecord
{
    public $rememberMe = true;
    public static function tableName()
    {
        return '{{%admin}}';
    }

    public function rules(){
        return [
            ['adminuser','required','message'=>'管理员账号不能为空'],
            ['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]);
    }

三、模板中链接的书写

<?php echo yii\helpers\Url::to(['public/logout']); ?>

四、session的移除

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

四、模板中ActiveForm的使用

<?php
    use yii\bootstrap\ActiveForm;
    use yii\helpers\Html;
?>
            <?php $from = ActiveForm::begin(['fieldConfig'=>['template'=>'{error}{input}']]); ?>
                <div class="span4 box">
                    <div class="content-wrap">
                        <h6>慕课商城 - 后台管理</h6>
                        <div class="form-group field-admin-adminuser">
                            <p class="help-block help-block-error"></p>
                            <?php echo $from->field($model,'adminuser')->textInput(['class'=>'span12','placeholder'=>'管理员账号']); ?>
                        </div>
                        <div class="form-group field-admin-adminpass">
                            <p class="help-block help-block-error"></p>
                            <?php echo $from->field($model,'adminpass')->passwordInput(['class'=>'span12','placeholder'=>'管理员密码']); ?>
                        </div>
                        <a href="/index.php?r=admin%2Fpublic%2Fseekpassword" class="forgot">忘记密码?</a>
                        <div class="form-group field-remember-me">
                            <?php echo $from->field($model,'rememberMe')->checkbox(['id'=>'remember-me','template'=>'<div class="remember">{input}<label for="remember-me">记住我</label></div>']); ?>
                        </div>
                        <?php echo Html::submitButton('登录',['class'=>'btn-glow primary login']); ?>
                </div>
            <!-- </form> -->
            <?php ActiveForm::end(); ?>
上一篇下一篇

猜你喜欢

热点阅读