CI框架代码体会

本文主要记录一些自己在浏览CI代码时候的一些体会, 并不分析CI的使用。

(http://codeigniter.org.cn/user_guide/toc.html  CI手册地址)

一 、 首先的一点就是, 已加载类在codeigniter, controller, model中的通用, 这使得CI的controller和model功能丰富。

体现在Controller中的构造函数中:

foreach (is_loaded() as $var => $class)
        {
            $this->$var =& load_class($class);   //将加载过的载入
        }

        $this->load =& load_class('Loader', 'core');

        $this->load->initialize();   // 加载autoload配置的库类

体现在model中:

function __get($key)
    {
        $CI =& get_instance();
        return $CI->$key;
    }

方法中

通过这样的操作, 是的CI的controller, model 功能丰富。

2、 CI允许扩展核心系统类:

在/system/core/common 中:

load_class 中有如下几行:

foreach (array(APPPATH, BASEPATH) as $path)
		{
			if (file_exists($path.$directory.'/'.$class.'.php'))
			{
				$name = $prefix.$class;

				if (class_exists($name) === FALSE)
				{
					require($path.$directory.'/'.$class.'.php');
				}

				break;
			}
		}

		// Is the request a class extension?  If so we load it too
		if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
		{
			$name = config_item('subclass_prefix').$class;

			if (class_exists($name) === FALSE)
			{
				require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
			}
		}

对于Controller的重写体现在(system/core/codeingiter):

require BASEPATH.'core/Controller.php';

    function &get_instance()
    {
        return CI_Controller::get_instance();
    }

    if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
    {
        require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
    }

这可以满足自己的业务逻辑的修改, 例如可以在自己扩展出来的controller中加一些权限控制的操作等等。

 

3、 Loader类也被controller加载了(依附在controller上的model也享受利益), 通过这个类,可以实现各种功能库的加载

 

4、 可以挂载多个系统

根据业务需求可能需要开发多个系统, 可以在index.php , 根据业务类型, 修改index.php

$applocation_folder参数

例如: 可以在配置虚拟主机的时候, 设置service_name 等等 当然这只是一种思路

(待续)

对CI的common文件比较纠结的就是, 在整个框架代码中, 突然出来一个这个理的方法还是有点让人不知所措, 觉得应该在方法的前面加上一个common_之类的标记, 然后自己也可以在里面创建一个自己的同类文件, 在方法的前面标记上my_,, 可能会更加清晰一些


转载于:https://my.oschina.net/u/195896/blog/80381

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值