Create your own Widget instance _ php instance in Yii

Source: Internet
Author: User
This article mainly introduces how to create your own widgets in Yii, and analyzes in detail the steps and implementation skills of creating widgets in Yii based on the specific instance form, for more information about how to create a Widget in Yii, see the following example. We will share this with you for your reference. The details are as follows:

Here, a random advertisement image is used as an example to describe the usage of widgets in Yii.

1. Call Widgets

The Code is as follows:

<? Php $ this-> widget ('widgetname');?>


Or

<? Php $ widget = $ this-> beginWidget ('path. to. widgetclass');?>... Body of content that may be obtained by small objects... <? Php $ this-> endWidget ();?>

You can also pass parameters to the Widget class.

<?php $userId = 1; ?><?php $this->widget('WidgetName',array('userId'=>$userId)); ?>

The userId parameter is automatically mapped to the Same Name property of the Widget class. Therefore, when defining a Widget, do not forget to declare this property.

2. Create a Widget

The custom Widget class inherits the CWidget and overwrites the run method.

<?phpclass BannerMagic extends CWidget {  public function run(){  }}

Or:

Class MyWidget extends CWidget {public function init () {// This method will be called by CController: beginWidget ()} public function run () {// This method will be called by CController :: endWidget () call }}

The following is the implementation of BannerMagicWidget.

<?php class BannerMagicWidget extends CWidget {  public function run() {   $random = rand(1,3);   if ($random == 1) {    $advert = "advert1.jpg";   } else if ($random == 2) {    $advert = "advert2.jpg";   } else {    $advert = "advert3.jpg";   }    $this->render('bannermagic',array(    "advert"=>$advert,   ));  }}

Stored in protected \ components \ BannerMagicWidget. php

The possible contents of the corresponding view File are as follows:

The Code is as follows:

"Alt =" whatever "/>


Stored in protected \ components \ views \ bannermagic. php

3. Call This Widget

The Code is as follows:

<? Php $ this-> widget ('bannermagicwidget ');?>

I hope this article will help you design PHP programs based on the Yii framework.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.