namespace app\controllers;
use Yii;
use yii\web\controller;
use app\models\regists;
use app\common\dayin;
class TapController extends Controller{
public function actionLogin(){
$model=new Regists;
if (dayin::ispost()) {
$res=dayin::post();
unset($res["regists"]['verifyCode']);
$where=$res["regists"];
$res= $model->select($where);
if($res){
$session = Yii::$app->session;
// 开启session
$session->open();
$session->set('user',$res);
// 关闭session
$session->close();
return $this->redirect("?r=regist/show");
}else{
return $this->redirect("?r=tap/login");
}
}else{
//登录页面
return $this->render('login',['model'=>$model]);
}
}
model层
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class regists extends ActiveRecord{
public $verifyCode;
public static function tableName(){
return 'regist';
}
public function select($where){
return regists::find()
->where($where)
->asarray()
->one();
}
public function show(){
return regists::find()->asarray()->all();
}
public function del($where){
return regists::deleteAll($where);
}
表单
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\captcha\Captcha;
$this->title = '登录';
?>
<h1><?= Html::encode($this->title) ?></h1>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->label('用户名') ?>
<?= $form->field($model,'pwd')->label('密码')?>
<?= $form->field($model, 'verifyCode')->label('验证码')->widget(Captcha::className(), [
'options'=>['placeholder'=>'验证码'],
'captchaAction' => 'tap/captcha',
'imageOptions'=>['style'=>'margin-top:-5px;',],
'template' => '<div class="row"><div class="col-lg-6">{input}</div><div class="col-lg-6">{image}</div></div>',
]) ?>
<?= Html::submitButton('Login') ?>
<?php ActiveForm::end(); ?>