Home php教程 PHP开发 Yii2 framework study notes (7) -- gii and GridView

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

Dec 30, 2016 am 10:07 AM

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')],
Copy after login

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;],
        ],
    ]); ?>
Copy after login

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;,  
],
Copy after login

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];
    }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps KB5012643 for Windows 11 breaks .NET Framework 3.5 apps May 09, 2023 pm 01:07 PM

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Dec 15, 2023 pm 05:35 PM

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks

How to fix 0xc0000135 error on Windows 11 (solve KB5013943 update issues) How to fix 0xc0000135 error on Windows 11 (solve KB5013943 update issues) May 11, 2023 am 08:28 AM

What is the 0xc0000135 error and why do I get it? According to official Microsoft documentation, the 0xc0000135 error code is related to .NetFramework issues. It seems that many applications that require .NetFramework3.5 to work do not work with the latest Windows 11 update. This is what causes the 0xc0000135 error code and you can resolve this issue by enabling .NetFramework3.5 on your PC. Most modern applications rely on the .NetFramework.dll file to run as expected in the background. but

See all articles