先练习使用Yii的demo blog。
* 修改数据库连接
-改为mysql连接,这个比较简单,就是改config/main.php里,代码都有的,取消掉注释就好。
* 使用Gii
-先在main.php里添加下列代码:
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'test'
)
)
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'test'
)
)
-访问index.php/gii,输入上面设置的密码,就可以使用了
-先建一个测试表tbl_test,注意要有一个主键
-使用Model Generator生成model/Test.php,再用Crud生成一controllers/TestController.php和views/test目录及其下文件,就可以访问index.php/test了。如果要访问index.php/admin还要按需改TestController.php里的accessRules(), 把
array('allow',
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
这里只有admin可以访问,把admin改为@是允许通过验证的帐号访问
* 自己动手添加一个Register功能
-在model里添加一个RegisterForm.php,格式可以参考已有的LoginForm.php,把要用的数据都声明出来: public $username...
-在rules()里声明规则,array('password', 'authenticate'),这条声明就可以在authenticate方法中写验证信息,如:
if (strlen($this->password) < 6) {
$this->addError('password','密码最短6位');
}
$this->addError('password','密码最短6位');
}
-添加一个public function register($username, $password, $email)方法,处理密码md5($password),连接数据库插入: $sql = "INSERT INTO {{user}}(username, password, email) VALUES ('$username', '$pwd', '$email')";
$command = Yii::app()->db->createCommand($sql);
$command->execute();
$command = Yii::app()->db->createCommand($sql);
$command->execute();
-完成了class,就可以用Gii直接自动生成页面了。。