总共有4个文件:
1、数据模型:reg.PHP
- <?php
- namespace appmodels;
- use yiibaseModel;
- use yiidbActiveRecord;
- class reg extends ActiveRecord
- {
- <span style="white-space:pre"> </span>//因为没有验证啥的,所以这里没有代码
- <span style="white-space:pre"> </span>//这里不能写和数据库中表的字段一致的属性,否则会在控制器中显示数据库内容时,显示这里的属性值,而不是数据库的内容值
- <span style="white-space:pre"> </span>//eg:public $name;
- }
2、控制器:HelloController.php
- <?php
- //如何启动这个控制文件:运行web/index.php?r=hello/hello即可
- //其中r代表参数,第一个hello是控制器的名字,第二个是动作的名字
- namespace appcontrollers;
- use yiiwebController;
- use appmodelstest;//数据模型的命名空间
- use appmodelsreg;
- class HelloController extends Controller
- {
- //编一个用户注册页面,提交数据到reg数据表
- public function actionReg()
- {
- $model=new reg;
- if(yii::$app->request->post())
- {
- <span style="white-space:pre"> </span>//获取用户输入的值
- $model->name=yii::$app->request->post('name');
- $model->password=yii::$app->request->post('password');
- //保存到数据库
- <span style="white-space:pre"> </span>$model->save();
- return this->render('reg_success',['model'=>
- }
- else
- {
- //var_dump(yii::$app->request->post());
- return this->render('reg',['model'=>
- }
- }
3、视图文件:reg.php和reg_success.php
reg.php
- <?php
- use yiihelpersHtml;
- use yiiwidgetsActiveForm;
- ?>
- <?php $form=ActiveForm::begin();?>
- name:<input type="text" name='name' ><br>
- password:<input type="text" name='password'>
- <div class='form-group'>
- <?=Html::submitButton('submit',['class'=>'btn btn-primary btn-block']) ?>
- </div>
- <?php ActiveForm::end();?>
reg_success.php
- <?php
- use yiihelpersHtml;
- ?>
- <p>are you entered the following information:</p>
- <ul>
- <li><label>Name:</label><?=Html::encode($model->name)?></li>
- <li><label>Password:</label><?=Html::encode($model->password)?></li>
- </ul>