Home  >  Article  >  php教程  >  Yii2 framework study notes (7) -- gii and GridView

Yii2 framework study notes (7) -- gii and GridView

黄舟
黄舟Original
2016-12-30 10:07:581301browse

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

Yii2 framework study notes (7) -- gii and GridView

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

Yii2 framework study notes (7) -- gii and GridView

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

Yii2 framework study notes (7) -- gii and GridView

##Click Next, click Generate, It can help us generate the basic framework.

Yii2 framework study notes (7) -- gii and GridView

Combined with the adminlte template we used before, add the link to manage users in the left panel.


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.

Yii2 framework study notes (7) -- gii and GridView

The default generated page is displayed. The fields are as follows, which may not be the fields we want to display. This requires us to modify the view

Yii2 framework study notes (7) -- gii and GridView

. Change the displayed fields to username, email, creation date, update Date, role (RBAC related).


Modify the configuration of the GridView control in \backend\views\user\index.php as follows,

<?= GridView::widget([
        &#39;dataProvider&#39; => $dataProvider,
        &#39;filterModel&#39; => $searchModel,
        &#39;columns&#39; => [
            [&#39;class&#39; => &#39;yii\grid\SerialColumn&#39;],
             &#39;username&#39;,
             &#39;email:email&#39;,
             [
             	&#39;attribute&#39; => &#39;status&#39;,
             	&#39;value&#39; => &#39;statusLabel&#39;,
    		 ],
             &#39;created_at:datetime&#39;,
             &#39;updated_at:datetime&#39;,
             [
             	&#39;label&#39; => &#39;Role&#39;,
             	&#39;value&#39; => function($model) {
             	$auth = \Yii::$app->authManager;
             	$roles = $auth->getRolesByUser($model->id);
             	$ret = &#39;&#39;;
	             	foreach ($roles as $role) {
	             		$ret .= &#39; &#39; . $role->name;
	             	}
             	return $ret;
        		},
        	],
            [&#39;class&#39; => &#39;yii\grid\ActionColumn&#39;],
        ],
    ]); ?>

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.

[  
              &#39;attribute&#39; => &#39;status&#39;,  
              &#39;value&#39; => &#39;statusLabel&#39;,  
],

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 => &#39;Deleted&#39;,
    		self::STATUS_ACTIVE => &#39;Active&#39;,
    	];
    }
    
    /**
     * @return string
     */
    public function getStatusLabel()
    {
    	return self::getStatusList()[$this->status];
    }

The modified display effect is as follows

Yii2 framework study notes (7) -- gii and GridView

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)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn