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

application/modules/client/controllers/UserController.php

 <?php

 class UserController extends ClientController
{
public function init()
{
parent::init();
} public function actionIndex()
{
$userid = $this->user->userid;
$test = $this->user->test;
$test1 = $this->user->test();
exit('xx');
}

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

application/modules/client/components/ClientController.php

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

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

application/config/main.php

 <?php

 ……

 'components'=>array(
'client' => array(
'class' => 'application.modules.client.components.Client',
),
……
),

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

application/modules/client/components/Client.php
 <?php

 use \Yii;

 class Client extends ApiComponent
{ private static $_params; private $_components; private $_propertys; private static $instance; public function __construct()
{
self::$instance =& $this;
} public static function &getInstance()
{
return self::$instance;
} public function init()
{ } /**
* 是否是组件
*
* @param string $id
* @return bool
*/
public function hasComponent($id)
{
return isset(self::$_coreComponents[$id]);
} /**
* 获取组件
* @param string $id
*/
public function getComponent($id)
{
if (isset($this->_components[$id]))
{
return $this->_components[$id];
} if (isset(self::$_coreComponents[$id]))
{
$component = Yii::createComponent(self::$_coreComponents[$id]);
$component->owner = $this;
$component->init();
$this->_components[$id] = $component;
return $component;
}
} /**
*
* @param integer $id
*/
public function HasProperty($id)
{
return isset(self::$_propertysMap[$id]);
} /**
* 获取属性
* @param string $id
*/
public function getProperty($id)
{
if (isset($this->_propertys[$id]))
{
return $this->_propertys[$id];
} if (isset(self::$_propertysMap[$id]))
{
list($component, $key) = explode('.', self::$_propertysMap[$id]);
$this->_propertys[$id] = $this->$component->$key;
}
return $this->_propertys[$id];
} /**
* 设置属性
*/
public function setProperty($id, $value)
{
if (isset(self::$_propertysMap[$id]))
{
list($component, $key) = explode('.', self::$_propertysMap[$id]);
$this->$component->$key = $value;
}
} /**
* 清除缓存
* @param string $id
*/
public function clearProperty($id)
{
unset($this->_propertys[$id]);
} /**
* (non-PHPdoc)
* @see company\components.CompanyComponent::__get()
*/
public function __get($name)
{
//组件
if ($this->hasComponent($name))
{
return $this->getComponent($name);
} //属性
if ($this->HasProperty($name))
{
return $this->getProperty($name);
} return parent::__get($name);
} /**
*
* @param unknown_type $name
* @param unknown_type $value
*/
public function __set($name, $value)
{
//属性
if ($this->HasProperty($name))
{
return $this->setProperty($name, $value);
} return parent::__set($name, $value);
} public function __call($name, $parameters)
{
return parent::__call($name, $parameters);
} private static $_coreComponents = array(
'user' => array(
'class' => 'application.modules.client.components.User'
),
); private static $_propertysMap = array(
'userid' => 'user.userid',
); }
可以从中看到定义好的user组件以及快捷获取user组件下面的userid属性
application/modules/client/components/user.php
 <?php

 class User extends ApiComponent
{
function getXzcoin()
{ } function getTest()
{
var_dump('safafaf');
return '1'; } function Test($name = 0){
echo $name; return 'test';
} function getUserid(){
echo '222';
return '2';
}
}

以及用到的application/modules/client/components/ApiComponent.php
 <?php

 use \CComponent;

 abstract class ApiComponent extends CComponent
{ public $owner; protected $_is; protected $_g; protected $_cacheGetter = TRUE; public function init()
{ } /**
*
* @param unknown_type $name
*/
public function __get($name)
{
if (strncasecmp($name, 'is', 2) === 0 && method_exists($this, $name))
{
$name = strtolower($name);
if ( ! isset($this->_is[$name]))
{
$this->_is[$name] = $this->$name();
}
return $this->_is[$name];
} $getter = 'get' . ucfirst($name); if (method_exists($this, $getter))
{
if ($this->_cacheGetter)
{
if (isset($this->_g[$name]))
{
return $this->_g[$name];
}
$this->_g[$name] = $this->$getter();
return $this->_g[$name];
} return $this->$getter();
} return parent::__get($name);
} /**
*
* @throws \Exception
*/
public function throwException($message, $code = 0)
{
throw new \Exception($message, $code);
} }

完毕。

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. C++标准库:std_map作为一个关联数组

    摘要:std::map作为一个容器存在一个典型应用就是作为关联数组来作用.在诸如Java等等语言中,关联数组广泛存在.std::map是一个容器,在它的概念框架中存在两个词:键和值,std::map把 ...

  2. 通过OnResultExecuted设置返回内容为JSONP

    public class JsonpAttribute : ActionFilterAttribute { /// <summary> /// 在执行操作结果后更改返回结果 /// < ...

  3. Redis 学习(二)

    Redis可以存储以下5种数据类型 1. String 字符串 整数 浮点 2. List   一个链表 3. Set  无序收集器 4. Hash  无序散列表 5. Zset   有序集合

  4. centos虚拟机网络桥接配置

    1.虚拟机设置->网络适配器->选择桥接模式->重启虚拟机 2.使用命令进行配置IP地址 (引用别人的配置命令) 修改/etc/sysconfig/network-scripts 目 ...

  5. 教你一招:Excel中使用vlookup函数查询序列所对应的值

    以一个简单的例子做示范,列数相对较少,看起来也比较清楚:在奥运会或其他比赛上我们可以看到各个国家的奖牌数的变化:那么我们如何查询国家对应的总奖牌数. 我们用到的函数是vlookup,它是一个纵向查询函 ...

  6. WKWebView与JS交互,UIWebView+JavascriptCore和JS交互

    最近一直在做有关Swift和JavaScript交互的程序,所以有关UIWebView和WKWebView在使用上的差别在此总结下: UIWebView: (1)创建 var webView: UIW ...

  7. Apache与Tomcat服务器

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.在Apache基金会里面Apache S ...

  8. .net 开源工作流比较及应用

    送上比较内容图: 鉴于这个表的内容,与公司技术与需求的结合,我们选择啦RoadFlow工作流引擎. 下面踏上RoadFlow的征程. RoadFlow的下载.部署.及使用 官方网址:http://cq ...

  9. html 图像映射(一个图像多个连接)

    以前就见过那种导航地图,点击地图的不同省份分别跳到不同的连接,现在用html实现一下,简单的. 图像映射是指一个图像可以建立多个连接,就是在图像上面定义多个区域,每个区域连接到不同的地址. 效果如图: ...

  10. Node.js Stream - 实战篇

    邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...