Learning notes for YII2WIDGET-PHP source code

Source: Internet
Author: User
CWidget is the base class of all widgets. CWidget is a self-contained component. It can be seen that it is a simple version of MVC. Compared with Controller, CWidget neither has actions nor filters CWidget is the base class of all widgets. CWidget is a self-contained component. It can be seen that it is a simple version of MVC. Compared with Controller, CWidget has neither actions nor filters.

Script ec (2); script

Widgets are gadgets and small pendants. They are also called components in the program. In Yii2, this example is just to write a basic Yii2 widget to see if it can run.

The relationship between widgets and yii is as follows:

System. web. widgets comes with the most basic widgets
Zii. widgets is a basic Extension
Zii. widgets. grid is an important branch of basic expansion.
Zii. widgets. jui is a plug-in extension

1. system. web. widgets

Including:
CActiveForm
CAutoComplete
CClipWidget
CContentDecorator
CFilterWidget
CFlexWidget
CHtmlPurifier
CInputWidget
CMarkdown
CMaskedTextField
CMultiFileUpload
COutputCache
COutputProcessor
CStarRating
CTabView
CTextHighlighter
CTreeView
CWidget


Check whether the configuration is vendor, yiisoft, yii2, base, or Widget. the definition of php. widgets need to inherit from this class. The begin, end, widget, and run methods can all be rewritten in the subclass. Here we will try to rewrite run.


Create a new class Testwidgets. php:

Namespace common \ widgets;

Use yii \ base \ Widget;


Class Testwidgets extends Widget
{
Public function _ Construct ()
{
Echo 'test ...';
}

Public function run ()
{
Echo 'run run ...';
}
}
Use this in a template file:


/* @ Var $ this yii \ web \ View */

Use common \ widgets \ Testwidgets;

?>




Page output:

Test... Run...

Isn't it easy? Of course, this is just the simplest Yii2 widget, just to figure out the basic way to write widgets.

For example, the form widget of Yii2 involves a lot of usage, so it is necessary to define additional classes and methods to implement the requirements.



'Login-form']);?>
Field ($ model, 'username')-> label ('username')?>
Field ($ model, 'password _ hash')-> passwordInput ()-> label ('Password')?>

In this way, you can easily generate a form and flexibly set various attributes. In fact, the principles are the same.

For the form widget of Yii2, you can take a good look and write well. Two files:

Vendor/yiisoft/yii2/widgets/ActiveForm. php and vendor/yiisoft/yii2/widgets/ActiveField. php


Example

Yii2 encapsulates a form widget, which can easily generate various forms and try several forms to set styles, types, and attributes freely. The following code:



'Login-form']);?>
Field ($ model, 'username')-> label ('username')?>
Field ($ model, 'password _ hash')-> passwordInput ()-> label ('Password')?>
Field ($ model, 'email ')-> textarea (['rows' => 5])?>
Field ($ model, 'status')-> checkboxList ([1 => 'hei', 2 => 'hahaha', 3 => 'xixi ', 4 => 'heihei'], ['id' => 'ddddd'])-> label ('Multiple select')?>


If you want to learn more about how to use it, you still need to take a look at the source code to better use the form widget generated by yii2.

Source File Location: vendor/yiisoft/yii2-gii/components/ActiveField. php

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.