Yii - 小部件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014559227/article/details/77533123

案例目录结构

--modules // 模块存放目录
  --WidgetTestModule //WidgetTestModule 模块
    --WidgetTestModule.php //WidgetTestModule 模块入口类
    --controllers    //控制器存放目录
      -- TestController.php  //TestController 控制器
    --views          //视图存放目录
      -- test        //控制器id
         -- index1.php //名为index1的视图文件
--components    //自定义组件存放目录
  -- CounrtyService   //国家服务
     --CountryService.php //组件接口类
     --realization //组件接口实现类存放目录
       --CountryServiceRealization1.php //对CountryService 接口的实现
  -- DbOper     //数据库操作
     -- DbOper.php //组件接口类
     -- realization//组件接口实现类存放目录
        -- DbRealization1.php 对DbOper 接口的实现
--widgets    //自定义小部件存放,目录
  --Test1Widget        //Test1Widget 小部件
    --Test1Widget.php // 小部件实现类
    --views    // 视图存放目录
      --run.php// 小部件视图应用文件

所有的小部件都要继承 Widget 这个类

app\widgets\Test1Widget

<?php
namespace app\widgets\Test1Widget;

use Yii;
use yii\helpers\Html;
use yii\base\Widget;

class Test1Widget extends Widget
{
    public $code;

    public function init()
    {
        parent::init();
    }

    public function run()
    {

        $data = Yii::$app->countryService->getCountry($this->code);



        return $this->render('run',array(
            'country'   =>  $data
        ));
    }

    /**
     * @Override
     * 覆盖父类 getViewPath() 方法 自定义视图文件存储目录
     * 这里这样定义后   run() 中$this->render()方法返回的视图路径就是类目录下面的views目录
     */
    public function getViewPath()
    {
        return __DIR__ . '/views';
    }

}

WidgetTestModule 模块测试自定义的小部件

WidgetTestModule.php

<?php

namespace app\modules\WidgetTestModule;

use yii\base\Module;

class WidgetTestModule extends Module
{

    public function init()
    {
        $this->controllerNamespace = 'app\modules\WidgetTestModule\controllers';
    }

}

模块控制器 TestController.php

<?php

namespace app\modules\WidgetTestModule\controllers;

use yii\web\Controller;

class TestController extends Controller
{

    public function actionIndex1()
    {
        return $this->renderPartial('index1');
    }

}

TestController 控制器actionIndex1() 需要的index1视图

<?php
use yii\helpers\Html;

use app\widgets\Test1Widget\Test1Widget; //载入Test1Widget 小部件
?>

<?php
    echo Test1Widget::widget(array(
        'code'  =>  'AM'
    ));
?>

CountryService 组件

<?php
namespace app\components\CountryService;

interface CountryService
{

    /**
     * 根据id 获得country中的一条数据
     * @param
     * String code 表id
     * @return
     * array data 查询出的数据
     */
    public function getCountry($code='');

}

实现类 CountryServiceRealization1

<?php

namespace app\components\CountryService\realization;

use Yii;
use app\components\CountryService\CountryService;

class CountryServiceRealization1 implements CountryService
{
    /**
     * interface @Overide
     */
    public function getCountry($code='',$fields='*')
    {
        if($code === '')
            return array();

        $sql = 'select ' . $fields . ' from yii_country where code=:code';

        $keyVal = array(
            ':code'     =>  $code
        );

        return Yii::$app->dbOper->fetch($sql,$keyVal);
    }
}

CountryService 与 组件DbOper产生了耦合关系,因为CountryService 的实现类 CountryServiceRealization1 在实现过程中 使用了DbOper 组件

DbOper 组件的使用看下我的这篇文章

yii_country 表的数据库结构

CREATE TABLE `yii_country` (
  `code` char(2) NOT NULL,
  `name` char(52) NOT NULL,
  `population` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后我们在浏览器中输入

http://host/index.php?r=WidgetTestModule/test/index1    访问这个方法就可以测试一下了。

猜你喜欢

转载自blog.csdn.net/u014559227/article/details/77533123