当前位置:首页 > 联系方式 >

CI框架源码阅读---------Router.php

作者:厦门七星音乐岛文化传播有限公司 发布时间:2017-09-09 10:21:00

CI框架源码阅读---------Router.php [php]

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

/**

* CodeIgniter

*

* An open source application development framework for PHP 5.1.6 or newer

*

* @package CodeIgniter

* @author ExpressionEngine Dev Team

* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.

* @license http://codeigniter.com/user_guide/license.html

* @link http://codeigniter.com

* @since Version 1.0

* @filesource

*/

// ------------------------------------------------------------------------

/**

* Router Class

*

* Parses URIs and determines routing

*

* @package CodeIgniter

* @subpackage Libraries

* @author ExpressionEngine Dev Team

* @category Libraries

* @link http://codeigniter.com/user_guide/general/routing.html

*/

class CI_Router {

/**

* Config class

* 配置

* @var object

* @access public

*/

var $config;

/**

* List of routes

* 路由列表,值来自APPPATH/config/route.php

* @var array

* @access public

*/

var $routes = array();

/**

* List of error routes

* 错误路由列表

* @var array

* @access public

*/

var $error_routes = array();

/**

* Current class name

* URI中的Controller

* @var string

* @access public

*/

var $class = '';

/**

* Current method name

* URI中显示调用的函数,默认为index()

* @var string

* @access public

*/

var $method = 'index';

/**

* Sub-directory that contains the requested controller class

* URI中现实的目录信息

* @var string

* @access public

*/

var $directory = '';

/**

* Default controller (and method if specific 确定的)

* 默认控制器

* @var string

* @access public

*/

var $default_controller;

/**

* Constructor

*

* Runs the route mapping function.

* 加载并实例化config类和URI类

*/

function __construct()

{

$this->config =& load_class('Config', 'core');

$this->uri =& load_class('URI', 'core');

log_message('debug', "Router Class Initialized");

}

// --------------------------------------------------------------------

/**

* Set the route mapping

*

* This function determines 确定,决心 what should be served based on the URI request,

* as well as any "routes" that have been set in the routing config file.

* 设置默认的路由信息,如果不存在控制器信息,则根据routes.php的设置来加载默认的控制器,

*

* @access private

* @return void

*/

function _set_routing()

{

// Are query strings enabled in the config file? Normally CI doesn't utilize 运用 query strings

// since URI segments are more search-engine friendly, but they can optionally 视情况 be used.

// If this feature is enabled, we will gather the directory/class/method a little differently

// 如果项目是允许通过query_strings的形式,并且有通过$_GET的方式请求控制器的话,则以query_string形式路由

// 上面这里为什么还要判断有没有通过get的方式指定控制器?

// 其实是因为如果允许query_string的形式请求路由,但是却没有在APPPATH/config/config.php下配置

// controller_trigger,function_trigger,directory_trigger这三项的话,也是不能使用query_strings形式的

// 此时,我们依然会采用“段”的形式。

$segments = array();

if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))

{

//取得目录名,控制名和方法名传递的变量名。这三项都是在config/config.php里面定义的。

if (isset($_GET[$this->config->item('directory_trigger')]))

{

$this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));

$segments[] = $this->fetch_directory();

}

if (isset($_GET[$this->config->item('controller_trigger')]))

{

$this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));

$segments[] = $this->fetch_class();

}

if (isset($_GET[$this->config->item('function_trigger')]))

{

$this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));

$segments[] = $this->fetch_method();

}

}

// Load the routes.php file.

// 根据当前环境加载APPPATH下面的routes.php

if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))

{

include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');

}

elseif (is_file(APPPATH.'config/routes.php'))

{

include(APPPATH.'config/routes.php');

}

// 下面的这个$route变量是在routes.php中定义的用来设置默认的控制器和默认的404页面

$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;

unset($route);// 利用完就干掉,过河拆桥,毫不留情。

// Set the default controller so we can display it in the event事件

// the URI doesn't correlated 相关的 to a valid 有效的 controller.

// 根据刚才的配置信息,设定默认控制器,没有的话,就为FLASE。

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站建设公司 https://www.jingchucn.com/zt/wuhan_wangzhanjianshe/

  • 上一篇:浅谈 PHP 变量可用字符
  • 下一篇:最后一页
  • 

    COPYRIGHT © 2015 厦门七星音乐岛文化传播有限公司 ALL RIGHTS RESERVED.

    本站所有原创信息,未经许可请勿任意转载或复制使用

    网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100