Ci 同时访问多数据库


database.php 增加demo的配置文件

$db['demo']['hostname'] = 'localhost';
$db['demo']['username'] = 'root';
$db['demo']['password'] = '';
$db['demo']['database'] = 'demo';
$db['demo']['dbdriver'] = 'mysql';
$db['demo']['dbprefix'] = 'd_';
$db['demo']['pconnect'] = FALSE;
$db['demo']['db_debug'] = TRUE;
$db['demo']['cache_on'] = FALSE;
$db['demo']['cachedir'] = '';
$db['demo']['char_set'] = 'utf8';
$db['demo']['dbcollat'] = 'utf8_general_ci';
$db['demo']['swap_pre'] = '';
$db['demo']['autoinit'] = TRUE;
$db['demo']['stricton'] = FALSE;


Class demo(){
   private $newdb;
    
   /**
   *实例化新的数据库实例
   */
   public function __construct(){
       $this->newdb = $this->load->database('demo');
   } 
   
   /**
   *获取全部数据
   *@return object 
   */
   public function get_all(){
      $this->newdb->get('demotable')->result();
   }

}



Ci CI_DB类

一直看到 pdo类中有 CI_DB_pdo_driver extends CI_DB 有这个一个写法,可是怎么找都找不到这个CI_DB类
后来仔细一看.他妈的坑爹啊

在 system/database/DB.php 中有这么两句话,我才知道了写在了DB.php这个文件中

	       if ( ! class_exists('CI_DB'))
		{
			eval('class CI_DB extends CI_DB_active_record { }');
                }

OR下面这段代码.因为他判断是否要启动 ar类


               if ( ! class_exists('CI_DB'))
		{
			eval('class CI_DB extends CI_DB_driver { }');
		}

CI 支持HMVC

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
先来说说使用方法 ,原码非本人开发
在application目录下面创建modules
modules/模块名/controllers/模块名.php
modules/模板名/models/模块名.php
modules/模板名/config/route.php 重写路由,当然你也要定义其他的配置文件比如config.php

然后此目录下面比如我要创建一个产品模板
modules/product/controllers/product.php
modules/product/models/product.php

下载代码放application
http://blog.ssh110.com/wp-content/uploads/2012/04/third_party.zip

改变application/config/autoload.php

$autoload['packages'] = array(APPPATH.'third_party');

/**
以下方法被重写
MX_Controller
MX_Router
MX_Loader
MX_Config
MX_Lang

以上是重写的几个方法,原是以CI_开头的,现已改成MX_开头。如果要继续应该继承MX_开头的
比如我要重写CI_Controller

require APPPATH.'third_party/MX/Controller.php';
Class MY_Controller extends MX_Controller
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function is_login()
	{
		
	}
	
}

*/

创建文件 application/core/MY_Router.php

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Router class */
require APPPATH.'third_party/MX/Router.php';

class MY_Router extends MX_Router {

    public function _parse_routes() {
        //因为CI默认只载入application下config的routes.php文件
        //而放在modules里的routes.php需要先路由到该module才会被加载
        //这里修改成自动加载所有modules下的routes.php
        foreach(Modules::$locations as $location => $offset) {
            $modules_path = FCPATH . $location;
            $modules_dir = dir($modules_path);
            while ($file = $modules_dir->read()) {
                if (is_dir($modules_path . $file) AND $file != '.' AND $file != '..') {
                    $routes_file = "{$modules_path}{$file}/config/routes.php";
                    if (is_file($routes_file)) {
                        @include($routes_file);
                        $this->routes = array_merge($this->routes, $route);
                        unset ($route);
                    }
                }
            }
        }
        parent::_parse_routes();
    }

    //把验证不通过的都扔给site/controllers/router处理
    public function _validate_request($segments) {

        if (count($segments) == 0) return $segments;

        /* locate module controller */
        if ($located = $this->locate($segments)) return $located;

        return $this->locate(array(
            'site',
            'router',
            'index',
            $this->uri->uri_string
        ));

    }
}