Gii is an artifact in yii2. Using it can save a lot of development time.
Now use it to build a user management interface.
The route of Gii is gii, so enter index.php?r=gii to enter the Gii homepage, as follows
The basic crud interface uses Model Generator It can be completed with CRUD Generator.
Because User is the built-in Model of Yii, we no longer need to generate it, but we can take a look at the Model Generator interface
Just enter the table name, Other fields will be filled in automatically. After clicking Next, you can preview the generated file, and then click Generate to automatically generate it.
CRUD Generator can help us generate the V (View) and C (Controller) parts in MVC. The interface is as follows
Add menu statements to \backend\views\layouts\left.php
['label' => 'Manage Users', 'icon' => 'fa fa-users', 'url' => ['user/index'], 'visible' => Yii::$app->user->can('managerUser')],The icon uses fonts awesome(http://fontawesome.io/) The font icons have the same function as Glyphicons in bootstrap, but have more icons.
url is the route to be pointed to.
Visible is whether to display or not. The judgment is based on the previous permissions in rbac. The permissions are used to display the page.
Modify the configuration of the GridView control in \backend\views\user\index.php as follows,
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'username', 'email:email', [ 'attribute' => 'status', 'value' => 'statusLabel', ], 'created_at:datetime', 'updated_at:datetime', [ 'label' => 'Role', 'value' => function($model) { $auth = \Yii::$app->authManager; $roles = $auth->getRolesByUser($model->id); $ret = ''; foreach ($roles as $role) { $ret .= ' ' . $role->name; } return $ret; }, ], ['class' => 'yii\grid\ActionColumn'], ], ]); ?>where email:email is a conventional format of girdview, and the complete one is attribute:format :label, the last two parts of which do not need to be provided.
For the types supported by the second format, you can view the methods starting with as in \yii\i18n\Formatter. For example, email calls the asEmail method of Formatter.
[ 'attribute' => 'status', 'value' => 'statusLabel', ],This is used for customized display, display/sorting of attribute user gridview header, etc. Value is the specific displayed content. In this example, the model (common\models\User) is called. The getStatusLabel method in will display the return value. value can also use anonymous methods, such as the
getStatusLabel method used in the Role field is as follows
/** * @return array */ public static function getStatusList() { return [ self::STATUS_DELETED => 'Deleted', self::STATUS_ACTIVE => 'Active', ]; } /** * @return string */ public function getStatusLabel() { return self::getStatusList()[$this->status]; }The modified display effect is as follows The above are the Yii2 framework study notes (7) - the content of gii and GridView. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!