这是第一种方法,还有另一种方法,其实都差不多。
1.在创建widgetsHelloWiget.php
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Administrator 5 * Date: 2016/08/05 6 * Time: 15:55 7 */ 8 namespace appwidgets; 9 10 use yiiaseWidget; 11 12 class HelloWidget extends Widget 13 { 14 public $msg = ''; 15 16 //初始化 17 public function init() 18 { 19 parent::init(); // TODO: Change the autogenerated stub 20 echo '<ul>'; 21 } 22 23 //覆盖 24 public function run() 25 { 26 echo '</ul>'; 28 } 29 30 public function helloWidget($helloTest){ 31 return '<li>'.$helloTest.'</li>'; 32 } 33 34 }
2.在视图中调用
说明:创建好widget后,可以在其他任意视图中进行调用
例如在views/test/index.php中
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: moka同学 5 * Date: 2016/08/05 6 * Time: 16:15 7 */ 8 use appwidgetsHelloWidget;11 ?> 12 <?php $hello = HelloWidget::begin();?> 13 <?=$hello->helloWidget('hello1');?> 14 <?=$hello->helloWidget('hello2');?> 15 <?php HelloWidget::end();?>
注:~
转载请注明出处。QQ1727718211