模块的定义就不写了,直接进入主题看目录和文件:

application/modules/client/controllers/UserController.php

  1. <?php
  2.  
  3. class UserController extends ClientController
  4. {
  5. public function init()
  6. {
  7. parent::init();
  8. }
  9.  
  10. public function actionIndex()
  11. {
  12. $userid = $this->user->userid;
  13. $test = $this->user->test;
  14. $test1 = $this->user->test();
  15. exit('xx');
  16. }

$this->user找不到,它会去ClientController中找

application/modules/client/components/ClientController.php

  1. <?php
  2. class ClientController extends CController
  3. {
  4. public function init()
  5. {
  6. header('Content-type:text/html;charset=utf-8');
  7. Yii::import('ext.functions', true); //加载公共函数
  8. //$this->getUserId();
  9. }
  10. public function __get($name)
  11. {
  12. return Yii::app()->client->$name;
  13. }

找到__get($name)方法,返回的是全局的client组件

application/config/main.php

  1. <?php
  2.  
  3. ……
  4.  
  5. 'components'=>array(
  6. 'client' => array(
  7. 'class' => 'application.modules.client.components.Client',
  8. ),
  9. ……
  10. ),

配置好client组件,然后指定到

  1. application/modules/client/components/Client.php
  1. <?php
  2.  
  3. use \Yii;
  4.  
  5. class Client extends ApiComponent
  6. {
  7.  
  8. private static $_params;
  9.  
  10. private $_components;
  11.  
  12. private $_propertys;
  13.  
  14. private static $instance;
  15.  
  16. public function __construct()
  17. {
  18. self::$instance =& $this;
  19. }
  20.  
  21. public static function &getInstance()
  22. {
  23. return self::$instance;
  24. }
  25.  
  26. public function init()
  27. {
  28.  
  29. }
  30.  
  31. /**
  32. * 是否是组件
  33. *
  34. * @param string $id
  35. * @return bool
  36. */
  37. public function hasComponent($id)
  38. {
  39. return isset(self::$_coreComponents[$id]);
  40. }
  41.  
  42. /**
  43. * 获取组件
  44. * @param string $id
  45. */
  46. public function getComponent($id)
  47. {
  48. if (isset($this->_components[$id]))
  49. {
  50. return $this->_components[$id];
  51. }
  52.  
  53. if (isset(self::$_coreComponents[$id]))
  54. {
  55. $component = Yii::createComponent(self::$_coreComponents[$id]);
  56. $component->owner = $this;
  57. $component->init();
  58. $this->_components[$id] = $component;
  59. return $component;
  60. }
  61. }
  62.  
  63. /**
  64. *
  65. * @param integer $id
  66. */
  67. public function HasProperty($id)
  68. {
  69. return isset(self::$_propertysMap[$id]);
  70. }
  71.  
  72. /**
  73. * 获取属性
  74. * @param string $id
  75. */
  76. public function getProperty($id)
  77. {
  78. if (isset($this->_propertys[$id]))
  79. {
  80. return $this->_propertys[$id];
  81. }
  82.  
  83. if (isset(self::$_propertysMap[$id]))
  84. {
  85. list($component, $key) = explode('.', self::$_propertysMap[$id]);
  86. $this->_propertys[$id] = $this->$component->$key;
  87. }
  88. return $this->_propertys[$id];
  89. }
  90.  
  91. /**
  92. * 设置属性
  93. */
  94. public function setProperty($id, $value)
  95. {
  96. if (isset(self::$_propertysMap[$id]))
  97. {
  98. list($component, $key) = explode('.', self::$_propertysMap[$id]);
  99. $this->$component->$key = $value;
  100. }
  101. }
  102.  
  103. /**
  104. * 清除缓存
  105. * @param string $id
  106. */
  107. public function clearProperty($id)
  108. {
  109. unset($this->_propertys[$id]);
  110. }
  111.  
  112. /**
  113. * (non-PHPdoc)
  114. * @see company\components.CompanyComponent::__get()
  115. */
  116. public function __get($name)
  117. {
  118. //组件
  119. if ($this->hasComponent($name))
  120. {
  121. return $this->getComponent($name);
  122. }
  123.  
  124. //属性
  125. if ($this->HasProperty($name))
  126. {
  127. return $this->getProperty($name);
  128. }
  129.  
  130. return parent::__get($name);
  131. }
  132.  
  133. /**
  134. *
  135. * @param unknown_type $name
  136. * @param unknown_type $value
  137. */
  138. public function __set($name, $value)
  139. {
  140. //属性
  141. if ($this->HasProperty($name))
  142. {
  143. return $this->setProperty($name, $value);
  144. }
  145.  
  146. return parent::__set($name, $value);
  147. }
  148.  
  149. public function __call($name, $parameters)
  150. {
  151. return parent::__call($name, $parameters);
  152. }
  153.  
  154. private static $_coreComponents = array(
  155. 'user' => array(
  156. 'class' => 'application.modules.client.components.User'
  157. ),
  158. );
  159.  
  160. private static $_propertysMap = array(
  161. 'userid' => 'user.userid',
  162. );
  163.  
  164. }
  1. 可以从中看到定义好的user组件以及快捷获取user组件下面的userid属性
  1. application/modules/client/components/user.php
  1. <?php
  2.  
  3. class User extends ApiComponent
  4. {
  5. function getXzcoin()
  6. {
  7.  
  8. }
  9.  
  10. function getTest()
  11. {
  12. var_dump('safafaf');
  13. return '1';
  14.  
  15. }
  16.  
  17. function Test($name = 0){
  18. echo $name; return 'test';
  19. }
  20.  
  21. function getUserid(){
  22. echo '222';
  23. return '2';
  24. }
  25. }
  1.  
  2. 以及用到的application/modules/client/components/ApiComponent.php
  1. <?php
  2.  
  3. use \CComponent;
  4.  
  5. abstract class ApiComponent extends CComponent
  6. {
  7.  
  8. public $owner;
  9.  
  10. protected $_is;
  11.  
  12. protected $_g;
  13.  
  14. protected $_cacheGetter = TRUE;
  15.  
  16. public function init()
  17. {
  18.  
  19. }
  20.  
  21. /**
  22. *
  23. * @param unknown_type $name
  24. */
  25. public function __get($name)
  26. {
  27. if (strncasecmp($name, 'is', 2) === 0 && method_exists($this, $name))
  28. {
  29. $name = strtolower($name);
  30. if ( ! isset($this->_is[$name]))
  31. {
  32. $this->_is[$name] = $this->$name();
  33. }
  34. return $this->_is[$name];
  35. }
  36.  
  37. $getter = 'get' . ucfirst($name);
  38.  
  39. if (method_exists($this, $getter))
  40. {
  41. if ($this->_cacheGetter)
  42. {
  43. if (isset($this->_g[$name]))
  44. {
  45. return $this->_g[$name];
  46. }
  47. $this->_g[$name] = $this->$getter();
  48. return $this->_g[$name];
  49. }
  50.  
  51. return $this->$getter();
  52. }
  53.  
  54. return parent::__get($name);
  55. }
  56.  
  57. /**
  58. *
  59. * @throws \Exception
  60. */
  61. public function throwException($message, $code = 0)
  62. {
  63. throw new \Exception($message, $code);
  64. }
  65.  
  66. }

完毕。

yii模块下面的组件的更多相关文章

  1. yii中的自定义组件

    yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...

  2. Angular06 组件、模块、父子组件之间的数据传递

    1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...

  3. 每天记录一点:NetCore获得配置文件 appsettings.json vue-router页面传值及接收值 详解webpack + vue + node 打造单页面(入门篇) 30分钟手把手教你学webpack实战 vue.js+webpack模块管理及组件开发

    每天记录一点:NetCore获得配置文件 appsettings.json   用NetCore做项目如果用EF  ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如S ...

  4. 基于SqlSugar的开发框架循序渐进介绍(12)-- 拆分页面模块内容为组件,实现分而治之的处理

    在早期的随笔就介绍过,把常规页面的内容拆分为几个不同的组件,如普通的页面,包括列表查询.详细资料查看.新增资料.编辑资料.导入资料等页面场景,这些内容相对比较独立,而有一定的代码量,本篇随笔介绍基于V ...

  5. Auth模块、Forms组件

    Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...

  6. 使用模块定义AngularJS组件

    一.模块创建/查找 module 当创建一个模块时,必须指定name和requires参数,即使你的模块并不存在依赖 var myApp=angular.module("exampleApp ...

  7. vue中什么是模块 什么是组件?

    模块: 封装好的应用程序,它只是js文件的封装. 组件: 一个完整的单位个体,可以有js可以有css和html. 作者:晋飞翔手机号(微信同步):17812718961希望本篇文章 能给正在学习 前端 ...

  8. yii url美化 urlManager组件

    yii的官方文档对此的解释如下: urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...

  9. 关于yii的日志路由组件的配置问题

    最近突然意识到日志是很好滴debug工具,所以研究了一下yii的日志配置,想想应该还会有像我这样的小白不懂这些问题的,就分享一下了.有错误烦请大神们指出config/main.php 中配置,这个想必 ...

随机推荐

  1. ModernUI教程:独立显示器DPI感知

             独立显示器DPI感知,是在Windows 8.1中新增的特性,这个特性针对拥有多个显示器同时各个显示器的DPI设定又不同的人.对这个新特性做了优化支持的软件能够在一个高DPI的显示器 ...

  2. [转]struts2处理.do后缀的请求

    原文地址:http://skyuck.iteye.com/blog/545988 默认情况下,struts2是无法处理以.do为后缀的请求url的(默认情况下是.action或者不填,可以参见org. ...

  3. Pyhont-Urllib2

    Urllib2 相当于的Urllib  的升级版 但又不能代替 Urllib 这个我得新手很费解呢...这个问题留着把,,等以后成大牛的时候在回来想想这儿问题!! Urllib2 常用 // 1 设置 ...

  4. Java基础-重写方法

    一般我们需要在新类上重写,两个类的实现: class Animal{ public void move(){ System.out.println("动物可以移动"); } } c ...

  5. 原生JS常用代码汇总

    数组相关 var codes = new Array( ); //创建数组codes.length //数组长度 动态插入数组 codes.push(value);

  6. ActiveMQ启动多个broker

    具体步骤如下: 1.把activemq目录下的conf文件复制一份,叫做conf2, 命令: cp -r conf conf2 2.修改conf2目录下的activemq.xml文件 a.修改brok ...

  7. Android基础篇(一)

    Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...

  8. 如何应对ISP乱插广告(案例分析)

    一.广告从何而来? 利益让人铤而走险,从而推动行业“发展”:广告的利益还真不小,xx房产门户网站上一个广告位少则几千,多则几十万:记得在校读书的时候,刚学会做网站,第一想法就是等自己的网站发展成熟有人 ...

  9. Windows - 杀死占用某个端口号的进程

    Windows不像Linux,Unix那样,ps -ef 查出端口和进程号,然后根据进程号直接kill进程. Windows根据端口号杀死进程要分三步: 第一步 根据 端口号 寻找 进程号 C:\&g ...

  10. PhpStorm 9.03 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项

    第一步:配置 git.exe File -> Default Settings -> Version Control -> Git -> Path go Git executa ...