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 中配置,这个想必 ...
随机推荐
- ModernUI教程:独立显示器DPI感知
独立显示器DPI感知,是在Windows 8.1中新增的特性,这个特性针对拥有多个显示器同时各个显示器的DPI设定又不同的人.对这个新特性做了优化支持的软件能够在一个高DPI的显示器 ...
- [转]struts2处理.do后缀的请求
原文地址:http://skyuck.iteye.com/blog/545988 默认情况下,struts2是无法处理以.do为后缀的请求url的(默认情况下是.action或者不填,可以参见org. ...
- Pyhont-Urllib2
Urllib2 相当于的Urllib 的升级版 但又不能代替 Urllib 这个我得新手很费解呢...这个问题留着把,,等以后成大牛的时候在回来想想这儿问题!! Urllib2 常用 // 1 设置 ...
- Java基础-重写方法
一般我们需要在新类上重写,两个类的实现: class Animal{ public void move(){ System.out.println("动物可以移动"); } } c ...
- 原生JS常用代码汇总
数组相关 var codes = new Array( ); //创建数组codes.length //数组长度 动态插入数组 codes.push(value);
- ActiveMQ启动多个broker
具体步骤如下: 1.把activemq目录下的conf文件复制一份,叫做conf2, 命令: cp -r conf conf2 2.修改conf2目录下的activemq.xml文件 a.修改brok ...
- Android基础篇(一)
Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...
- 如何应对ISP乱插广告(案例分析)
一.广告从何而来? 利益让人铤而走险,从而推动行业“发展”:广告的利益还真不小,xx房产门户网站上一个广告位少则几千,多则几十万:记得在校读书的时候,刚学会做网站,第一想法就是等自己的网站发展成熟有人 ...
- Windows - 杀死占用某个端口号的进程
Windows不像Linux,Unix那样,ps -ef 查出端口和进程号,然后根据进程号直接kill进程. Windows根据端口号杀死进程要分三步: 第一步 根据 端口号 寻找 进程号 C:\&g ...
- PhpStorm 9.03 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项
第一步:配置 git.exe File -> Default Settings -> Version Control -> Git -> Path go Git executa ...