[moka同学笔记]yii2.0小物件的简单使用(第二种方法)

时间:2021-10-24 16:51:25

1.在widgets/TestWidget.php中

 <?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/08/05
* Time: 18:15
*/
namespace app\widgets; use yii\base\Widget; class TestWidget extends Widget
{
public $msg = ''; //初始化
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
} //覆盖
public function run()
{
return $this->render('index',['msg'=>$this->msg]);
}
}

2.在widgets/views/index.php

 <?php
/**
* Created by PhpStorm.
* User:moka同学
* Date: 2016/08/05
* Time: 18:20
*/
echo $msg.'这个是公共部分';
?>

3.在其他需要使用的视图文件中 如下调用即可

<?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/08/05
* Time: 16:15
*/
use app\widgets\TestWidget;
?>
<?= TestWidget::widget();?>

注:~

转载请注明出处。QQ1727728211