Yii
来源:互联网 发布:无经验美工如何面试 编辑:程序博客网 时间:2024/05/16 09:03
案例目录结构
--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
<?phpnamespace 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
<?phpnamespace app\modules\WidgetTestModule;use yii\base\Module;class WidgetTestModule extends Module{ public function init() { $this->controllerNamespace = 'app\modules\WidgetTestModule\controllers'; }}
模块控制器 TestController.php
<?phpnamespace app\modules\WidgetTestModule\controllers;use yii\web\Controller;class TestController extends Controller{ public function actionIndex1() { return $this->renderPartial('index1'); }}
TestController 控制器actionIndex1() 需要的index1视图
<?phpuse yii\helpers\Html;use app\widgets\Test1Widget\Test1Widget; //载入Test1Widget 小部件?><?php echo Test1Widget::widget(array( 'code' => 'AM' ));?>
CountryService 组件
<?phpnamespace app\components\CountryService;interface CountryService{ /** * 根据id 获得country中的一条数据 * @param * String code 表id * @return * array data 查询出的数据 */ public function getCountry($code='');}
实现类 CountryServiceRealization1
<?phpnamespace 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 访问这个方法就可以测试一下了。
阅读全文
0 0
- Yii
- yii
- Yii
- yii
- yii
- yii
- Yii
- yii
- yii
- yii
- yii
- YII
- Yii
- Yii
- Yii
- Yii
- Yii
- Yii
- gpio_direction_output vs gpio_set_value之间的使用关系
- c++学习----const常量折叠
- [OpenSource] Android 图片、视频选择库 PVSelector
- python实现找出来一个字符串中最长不重复子串
- MySQL SQL 数据排名查询某条数据是总数据的第几条
- Yii
- Oracle DB管理内存详细介绍
- A: 统计数字
- JDBC获取连接的三种工具类封装方式(v1.0)
- HIVE的动态分区插入
- tensorflow 中的embedding 报错问题解决
- Android 多线程延迟(sleep)
- Hadoop集群下进行Zookeeper安装
- 实体类JavaBean转Xml-JAXB的使用分享