search
Homephp教程PHP开发yii2.0 widget production tutorial

Widget production:

1. Take making a hello widget as an example. The following figure shows a simple directory structure (can be modified according to needs):

yii2.0 widget production tutorial

2. Open HelloWidget.php and add the following code:

<?php
namespace commonwidgetshello;

use yiiaseWidget;
class HelloWidget extends Widget
{
public $msg = &#39;&#39;;
/**
* 初始化
* @see yiiaseObject::init()
*/
public function init(){
parent::init(); 
}


public function run(){
return $this->render(&#39;index&#39;,[&#39;msg&#39;=>$this->msg]);
}
}

3. Open the views layer rendering page common/hello/views/index.php

//简单举例,此处可以设计视图
<?php
echo $msg;

4. Small To call the widget, add the following code to the views layer page

<?=commonwidgetshelloHelloWidget::widget([&#39;msg&#39;=>&#39;hello world&#39;])?>

5. Display effect

yii2.0 widget production tutorial

The role of widgets is that there are many repetitions in the project Small functions, such as image upload and text editor, can be made into widgets to improve the code reuse rate. The effect is significant. I personally think that the entire website can be broken into many widgets.

The above is the content of the yii2.0 widget production tutorial. 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools