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

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#扩展方法知多少

    前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识.看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法.博主打 ...

  2. 基于SoCkit的opencl实验1-基础例程

    基于SoCkit的opencl实验1-基础例程 准备软硬件 Arrow SoCkit Board 4GB or larger microSD Card Quartus II v14.1 SoCEDS ...

  3. Mbatis Oracle 第一次插入失败 useGeneratedKeys

    <insert id="insertAgentInfo" parameterType="pd" useGeneratedKeys="false& ...

  4. 小程序实现sql插入语句转换成Laravel迁移语句

    sql的插入语句长这样: INSERT INTO `media` (`MediaID`, `type`, `filename`, `title`) VALUES (1, 'word', 'word1. ...

  5. windows2003安装证书服务:csp配置不正确、您没有此密钥容器的写访问权限

    1.填写CA名称后在生成密钥时提示:csp配置不正确或安装不完整.      原因:可能的原因为CS服务(Crysptographic Service)没有启动 . ps:该服务依赖RPC服务,但RP ...

  6. Java代码执行顺序(静态变量,非静态变量,静态代码块,代码块,构造函数)加载顺序

    //据说这是一道阿里巴巴面试题,先以这道题为例分析下 public class Text { public static int k = 0; public static Text t1 = new ...

  7. CocoaPods第三方库管理 iOS

    越来越多的SVN管理,越来越多的工程文件,我不能总是那么一个一个的把第三方库拖进去,我厌倦了拖拽和配置,我找到了替代方法--CocoaPods 补充一下:最近在给新机子安装时 发现 sudo gem ...

  8. oracle---日期等plsql

    日期/时间 相关查询 1.获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 "SYSDATE"来指定查询的日期. SELECT TRUNC ( ...

  9. Logstash-5.0同步.json文件到ElasticSearch-5.0配置文件

    logstash/conf/input-file.conf内容如下: input { file { #监听文件的路径. path => ["E:/data_json/*.json&qu ...

  10. jQuery中 pageX,clientX,offsetX,layerX的区别

    一.PageX和clientXPageX和clientX ,这个两个比较容易搞混,PageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化.可以理解为:相对#(0.0 ...