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

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. 微信快速开发框架(八)-- V2.3--增加语音识别及网页获取用户信息,代码已更新至Github

    不知不觉,版本以每周更新一次的脚步进行着,接下来应该是重构我的代码及框架的结构,有朋友反应代码有点乱,确实如此,当时写的时候只是按照订阅号来写的,后来才慢慢增加到支持API接口.目前还在开发第三方微信 ...

  2. Codeforces Round #380(div 2)

    A. 题意:给你一串字符串(<=100),将ogo ogogo ogogogo ogogogogo……这种全部缩成***,输出缩后的字符串 分析:第一遍扫对于那些go的位置,记录下next[i] ...

  3. 【JavaScript】Html form 提交表单方式

    源:http://blog.csdn.net/wang02011/article/details/6299517 1.input[type='submit'] 2.input[type='image' ...

  4. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

  5. 由Memcached升级到 Couchbase的 Java 客户端的过程记录(二)

    Shiro提供了类似于Spring的Cache抽象,即Shiro本身不实现Cache,但是对Cache进行了又抽象,方便更换不同的底层Cache实现. shiro对缓存的支持 shiro并没有实现缓存 ...

  6. ob_start()

    ob_start()函数用于打开缓冲区 1.用于header()之前 ob_start(); //打开缓冲区 echo "Hellon"; //输出 header("lo ...

  7. java高新技术-操作javaBean

    1. 对javaBean的简单内省操作 public class IntroSpectorTest { public static void main(String[] args) throws Ex ...

  8. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  9. 防DDOS攻击SHELL脚本

    最近一段时间服务器频繁遭到DDOS攻击,目前只能通过封IP来源来暂时解决.IP不源变化多端,光靠手工来添加简直是恶梦,想了个方法,用SHELL来做. 比较简单,但很实用:) 以下内容根据作者原文进行适 ...

  10. maridb(mysql) debian-sys-maint用户说明

    debian-sys-maint中Debian系统对MySQL维护用的,可以理解为通过系统的某个“非常规”程序对Mysql进行备份恢复等行为时,改程序所使用的登录Mysql的账户. 这个debian- ...