codeigniter重写核心类URI的_set_uri_string,调用数据库

<?php
class MY_URI extends CI_URI {
	function __construct() {
		parent::__construct ();
	}
	function _set_uri_string($str) {
		// Filter out control characters
		$str = remove_invisible_characters ( $str, FALSE );
		
		$str = ($str == '/') ? '' : $str;
		
		include APPPATH . '/config/database.php';
		$conn = $this->db_connect ( $db ['default'] ['hostname'], $db ['default'] ['database'], $db ['default'] ['username'], $db ['default'] ['password'] );
		if ($conn) {
			if ($str) {
				$query = mysql_query ( "select system_url from url_rewrite where request_url='" . $str . "'" );
				$result = mysql_fetch_assoc ( $query );
				if ($result) {
					$str = $result ['system_url'];
				}
			}
		} else {
			log_message ( '数据库出错' );
		}
		
		// If the URI contains only a slash we'll kill it
		$this->uri_string = $str;
	}
	function db_connect($hostname, $database, $username, $password) {
		return mysql_select_db ( $database, mysql_connect ( $hostname, $username, $password ) );
	}
}

Read More