1. 创建管理员表
进入项目根目录,在根目录执行命令:
2. 创建管理的控制器
1
| $ cd console/controllers/
|
编写代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
namespace controllers;
use commonmodelsUserLoginToken; use Yii; use yiiController; use commonmodelsUser;
class InitController extends Controller {
public function actionAdmin()< 大专栏 Yii2创建管理员登录br/> { echo "创建一个新用户 ...n"; $username = $this->prompt('User Name:'); $email = $this->prompt('Email:'); $password = $this->prompt('Password:'); $model = new User(); $model->username = $username; $model->email = $email; $model->generateAuthKey(); $model->generatePasswordResetToken();
$model->password = $password; if (!$model->save()) { foreach ($model->getErrors() as $error) { foreach ($error as $e) { echo "$en"; } } return 1; } return 0; } }
|
3. 执行脚本
然后分别输入帐户、Email、登录密码,完成创建。