yii2.0實(shí)現(xiàn)創(chuàng)建簡(jiǎn)單widgets示例

字號(hào):


    本文實(shí)例講述了yii2.0實(shí)現(xiàn)創(chuàng)建簡(jiǎn)單widgets的方法。分享給大家供大家參考,具體如下:
    namespace yii\widgets;
    use yii\base\Widget;
    use yii\helpers\Html;
    class HelloWidget extends Widget
    {
    public $message;
    public function init()
    {
    parent::init();
    if ($this->message === null) {
    $this->message = 'Hello World';
    }
    }
    public function run()
    {
    return Html::encode($this->message);
    }
    }
    調(diào)用方法:
    <?php
    use app\components\HelloWidget;
    ?>
    <?= HelloWidget::widget(['message' => 'Good morning']) ?>
    希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。