yii模块下面的组件
模块的定义就不写了,直接进入主题看目录和文件:
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模块下面的组件的更多相关文章
- yii中的自定义组件
yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...
- Angular06 组件、模块、父子组件之间的数据传递
1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...
- 每天记录一点:NetCore获得配置文件 appsettings.json vue-router页面传值及接收值 详解webpack + vue + node 打造单页面(入门篇) 30分钟手把手教你学webpack实战 vue.js+webpack模块管理及组件开发
每天记录一点:NetCore获得配置文件 appsettings.json 用NetCore做项目如果用EF ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如S ...
- 基于SqlSugar的开发框架循序渐进介绍(12)-- 拆分页面模块内容为组件,实现分而治之的处理
在早期的随笔就介绍过,把常规页面的内容拆分为几个不同的组件,如普通的页面,包括列表查询.详细资料查看.新增资料.编辑资料.导入资料等页面场景,这些内容相对比较独立,而有一定的代码量,本篇随笔介绍基于V ...
- Auth模块、Forms组件
Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...
- 使用模块定义AngularJS组件
一.模块创建/查找 module 当创建一个模块时,必须指定name和requires参数,即使你的模块并不存在依赖 var myApp=angular.module("exampleApp ...
- vue中什么是模块 什么是组件?
模块: 封装好的应用程序,它只是js文件的封装. 组件: 一个完整的单位个体,可以有js可以有css和html. 作者:晋飞翔手机号(微信同步):17812718961希望本篇文章 能给正在学习 前端 ...
- yii url美化 urlManager组件
yii的官方文档对此的解释如下: urlSuffix 此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...
- 关于yii的日志路由组件的配置问题
最近突然意识到日志是很好滴debug工具,所以研究了一下yii的日志配置,想想应该还会有像我这样的小白不懂这些问题的,就分享一下了.有错误烦请大神们指出config/main.php 中配置,这个想必 ...
随机推荐
- 秀起来的coding
转载:http://www.zuidsaima.com/html/1695882735602688/index.html
- 【Python】[进程和线程]多进程,多线程,ThreadLocal,进程VS.线程,分布式进程
1.多进程,multiprocessing模块, 进程间的通信:Queue[队列],Pipes[管子]2.多线程, 注意:线程公用变量,混乱 解决方法Lock:因为只有一个锁,所以当要执 ...
- 扩展htmlhelper.DropDownListFor 支持list数据源和option增加属性
mvc自带的DropDownListFor数据源必须是IEnumerable<SelectListItem>.并且option不支持增加自定义属性.在使用bootstrap-select组 ...
- jQuery笔记总结
来源于:http://blog.poetries.top/2016/10/20/review-jQuery/ http://www.jianshu.com/p/f8e3936b34c9 首先,来了解一 ...
- 常见排序java实现
public class Sort { public static void main(String[] args) { int[] data = {49,38,65,97,76,13,27,49}; ...
- ASP.NET Identity 简介
翻译自:http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity ,略有改动. 背景:A ...
- angularJs模块ui-router之路由控制
在你的应用中大多数状态都有与其相关联的 url,路由控制不是设计完成 state 之后的事后想法,而是开始开发时就应该考虑的问题. 这里是如何设置一个基本url. $stateProvider .st ...
- 配置javac环境
初始的javac是默认不可用,如下图: 系统变量->新建->变量名:JAVA_HOME 变量值:(C:\Program Files\Java\jdk1.7.0_03)(这只是我的JDK安装 ...
- VsFtpd配置文件详解
1.默认配置:1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES2>匿名用户使用的登陆名为ftp或anonymou ...
- C#中的多态性
1.重载(overload) public void Sleep() { Console.WriteLine("Animal睡觉"); } public int Sleep(int ...