CodeIgniter 核心代码阅读-路由文件Router.php

Ubuntu 13.10 Apache虚拟主机配置全解析:权限、Require指令与Name-based VHost实战 Apache虚拟主机是Web服务路由与多站点隔离的基础机制,其核心原理在于HTTP请求头中的Host字段匹配与服务器端的Directory访问控制策略。在Linux系统中,它高度依赖文件权限模型(如www-data组)、模块化配置(a2ensite/a2enmod)及符合版本特性的语法规范。Ubuntu 13.10作为Apache 2.4与Upstart的关键过渡版本,强制要求使用Require all granted替代旧式Order/Allow指令,并需严格遵循基于名称的虚拟主机(Name-based 阅读详情

Router.php--CI路由文件

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

//URI解析和路由
class CI_Router {

	//config类
	var $config;

	//路由数组
	var $routes			= array();

	//错误路由数组
	var $error_routes	= array();

	//当前类名
	var $class			= '';

	//当前方法名
	var $method			= 'index';
	
        //控制器目录
	var $directory		= '';

	//默认控制器
	var $default_controller;

	//构造方法
	function __construct()
	{
		$this->config =& load_class('Config', 'core');
		$this->uri =& load_class('URI', 'core');
		log_message('debug', "Router Class Initialized");
	}

	//设置路由
	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
		$segments = array();
		if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
		{
			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.
		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');
		}

		$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.
		$this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);

		// Were there any query string segments?  If so, we'll validate them and bail out since we're done.
		if (count($segments) > 0)
		{
			return $this->_validate_request($segments);
		}

		// Fetch the complete URI string
		$this->uri->_fetch_uri_string();

		// Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
		if ($this->uri->uri_string == '')
		{
			return $this->_set_default_controller();
		}

		// Do we need to remove the URL suffix?
		$this->uri->_remove_url_suffix();

		// Compile the segments into an array
		$this->uri->_explode_segments();

		// Parse any custom routing that may exist
		$this->_parse_routes();

		// Re-index the segment array so that it starts with 1 rather than 0
		$this->uri->_reindex_segments();
	}

	//设置默认控制器
	function _set_default_controller()
	{
		if ($this->default_controller === FALSE)
		{
			show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
		}
		// Is the method being specified?
		if (strpos($this->default_controller, '/') !== FALSE)
		{
			$x = explode('/', $this->default_controller);

			$this->set_class($x[0]);
			$this->set_method($x[1]);
			$this->_set_request($x);
		}
		else
		{
			$this->set_class($this->default_controller);
			$this->set_method('index');
			$this->_set_request(array($this->default_controller, 'index'));
		}

		// re-index the routed segments array so it starts with 1 rather than 0
		$this->uri->_reindex_segments();

		log_message('debug', "No URI present. Default controller set.");
	}

	//设置路由
	function _set_request($segments = array())
	{
		$segments = $this->_validate_request($segments);

		if (count($segments) == 0)
		{
			return $this->_set_default_controller();
		}

		$this->set_class($segments[0]);

		if (isset($segments[1]))
		{
			// A standard method request
			$this->set_method($segments[1]);
		}
		else
		{
			// This lets the "routed" segment array identify that the default
			// index method is being used.
			$segments[1] = 'index';
		}

		// Update our "routed" segment array to contain the segments.
		// Note: If there is no custom routing, this array will be
		// identical to $this->uri->segments
		$this->uri->rsegments = $segments;
	}

	//验证路由数组,并设置控制器方法
	function _validate_request($segments)
	{
		if (count($segments) == 0)
		{
			return $segments;
		}

		// Does the requested controller exist in the root folder?
		if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
		{
			return $segments;
		}

		// Is the controller in a sub-folder?
		if (is_dir(APPPATH.'controllers/'.$segments[0]))
		{
			// Set the directory and remove it from the segment array
			$this->set_directory($segments[0]);
			$segments = array_slice($segments, 1);

			if (count($segments) > 0)
			{
				// Does the requested controller exist in the sub-folder?
				if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
				{
					if ( ! empty($this->routes['404_override']))
					{
						$x = explode('/', $this->routes['404_override']);

						$this->set_directory('');
						$this->set_class($x[0]);
						$this->set_method(isset($x[1]) ? $x[1] : 'index');

						return $x;
					}
					else
					{
						show_404($this->fetch_directory().$segments[0]);
					}
				}
			}
			else
			{
				// Is the method being specified in the route?
				if (strpos($this->default_controller, '/') !== FALSE)
				{
					$x = explode('/', $this->default_controller);

					$this->set_class($x[0]);
					$this->set_method($x[1]);
				}
				else
				{
					$this->set_class($this->default_controller);
					$this->set_method('index');
				}

				// Does the default controller exist in the sub-folder?
				if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
				{
					$this->directory = '';
					return array();
				}

			}

			return $segments;
		}


		// If we've gotten this far it means that the URI does not correlate to a valid
		// controller class.  We will now see if there is an override
		if ( ! empty($this->routes['404_override']))
		{
			$x = explode('/', $this->routes['404_override']);

			$this->set_class($x[0]);
			$this->set_method(isset($x[1]) ? $x[1] : 'index');

			return $x;
		}


		// Nothing else to do at this point but show a 404
		show_404($segments[0]);
	}

	//路由解析
	function _parse_routes()
	{
		// Turn the segment array into a URI string
		$uri = implode('/', $this->uri->segments);

		// Is there a literal match?  If so we're done
		if (isset($this->routes[$uri]))
		{
			return $this->_set_request(explode('/', $this->routes[$uri]));
		}

		// Loop through the route array looking for wild-cards
		foreach ($this->routes as $key => $val)
		{
			// Convert wild-cards to RegEx
			$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));

			// Does the RegEx match?
			if (preg_match('#^'.$key.'$#', $uri))
			{
				// Do we have a back-reference?
				if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
				{
					$val = preg_replace('#^'.$key.'$#', $val, $uri);
				}

				return $this->_set_request(explode('/', $val));
			}
		}

		// If we got this far it means we didn't encounter a
		// matching route so we'll set the site default route
		$this->_set_request($this->uri->segments);
	}

	//设置类名
	function set_class($class)
	{
		$this->class = str_replace(array('/', '.'), '', $class);
	}

	//获取类名
	function fetch_class()
	{
		return $this->class;
	}

	//设置方法名
	function set_method($method)
	{
		$this->method = $method;
	}

	//获取方法名
	function fetch_method()
	{
		if ($this->method == $this->fetch_class())
		{
			return 'index';
		}

		return $this->method;
	}

	//设置目录名你
	function set_directory($dir)
	{
		$this->directory = str_replace(array('/', '.'), '', $dir).'/';
	}

	//获得目录名称
	function fetch_directory()
	{
		return $this->directory;
	}

	//覆盖控制器
	function _set_overrides($routing)
	{
		if ( ! is_array($routing))
		{
			return;
		}

		if (isset($routing['directory']))
		{
			$this->set_directory($routing['directory']);
		}

		if (isset($routing['controller']) AND $routing['controller'] != '')
		{
			$this->set_class($routing['controller']);
		}

		if (isset($routing['function']))
		{
			$routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
			$this->set_method($routing['function']);
		}
	}


}



Mythos:可调度的零边际成本漏洞发现能力 漏洞发现正从依赖专家经验的手工审计,演变为基于大模型推理的确定性工程任务。其核心原理在于将软件语义、开发契约与安全规则内化为模型认知,通过测试时强化学习(test-time RL)实现多路径穷举验证与高置信度利用链生成。这种能力具备零边际成本、可集成进CI/CD、无需安全背景即可调用的技术价值,广泛适用于嵌入式固件审计、遗留系统渗透、开源组件合规检查等场景。Mythos所代表的,正是AI驱动下漏洞发现范式的根本迁移。 阅读详情

相关推荐

PHP简单实现MVC框架路由功能源码

PHP简单实现MVC框架路由功能源码

php ci 路由,CI3.0 默认路由问题 解决办法

当开查询字符串的时候,参数enable_query_strings 为 true 。首页报错An Error Was EncounteredUnable to determine what should be displayed. A default route has not been specified in the routing file.解决办地,修改文件 system/core/Rou...

weixin_33601966的博客 464

独立站如何采集商品?外贸独立站采集教程

从这里Robert了解到还有国内有很多仿牌卖家、微商,都有类似的情况,他们出海的阻碍主要在于支付、物流、推广。8.插件“抓取任务”列表手动点同步,将商品同步到主商城后台商品管理列表(商品默认同步到下架列表中) 后台商品管理下架列表显示已同步商品。5.店铺信息修改完成后,可以开始采集商品,采集时弹窗中会自动识别当前平台及商品链接,分类和相册名称可以自定义 编辑修改。4. 打开采集目标站商品页面,点开谷歌浏览器扩展程序列表,找到Shopyy采集平台,打开配置页。打开弹窗中的“店铺管理”,添加店铺信息。

Jernnifer_mao的博客 1379

CI框架 router.php,CI框架源码阅读---------Router.php_PHP教程

/*** 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, E...

weixin_39540020的博客 209

保姆级教程:用CST 2023一步步复现一个1-8GHz Vivaldi天线(含参数优化与结果分析)

本文提供了一份详细的CST 2023教程,指导读者如何从零开始设计并优化1-8GHz Vivaldi天线。内容涵盖工程设置、参数化建模、仿真加速技巧以及性能优化策略,特别适合需要实现宽带天线设计的工程师和研究人员。通过实战案例,帮助用户掌握Vivaldi天线的核心设计要点和CST高级功能应用。

weixin_30800987的博客 619

保姆级教程:手把手教你从ENSEMBL官网下载GRCh38/GRCh37的GTF注释文件(附网址规律总结)

本文提供从ENSEMBL官网下载GRCh38/GRCh37的GTF注释文件的详细教程,包括路径解析、版本号确定和下载命令示例。特别解析了GRCh37的特殊存储结构,并分享批量下载、文件校验等高级技巧,帮助生物信息学研究者高效获取基因组注释文件。

weixin_29289821的博客 204

router.php,CodeIgniter 源码分析之 Router.php

// ------------------------------------------------------------------------/*** Router Class*/class CI_Router {/*** Config class*/var $config;/*** List of routes*/var $routes = array();/*** List of ...

weixin_33268725的博客 274

机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾

f作者: 寒小阳 &&龙心尘 时间:2015年10月。 出处: 声明:版权所有,转载请注明出处,谢谢。手把手机器学习之逻辑回归应用——Kaggle泰坦尼克之灾1.引言先说一句,年末双十一什么的一来,真是非(mang)常(cheng)欢(gou)乐(le)!然后push自己抽出时间来写这篇blog的原因也非常简单: 写完前两篇逻辑回归的介绍和各个角度理解之后,小伙伴们纷纷表示『好像很高级的样纸,

寒小阳 43万+

CI中的默认访问的控制器

最近在学习CI框架,遇到了一个问题就是默认访问的控制器,CI框架中默认访问的控制器是welcome控制器,但是在项目开发中需要指定默认访问的网站的某个模块下的某个控制器,在网上查到说直接在配置文件中修改就可以,但是始终不行,最终在网上查到,好像是在3.1.4以上已经不支持修改访问模块下的控制器,需要自己修改系统文件中的内容:   将 if (empty($this-&gt;default_...

仰望星空下的自己 2936

计算机考研408复试题汇总

TCP连接是全双工的,两端都可以先挥手 其中一端完成数据发送任务后,发送一个FIN包来终止,另一端针对这个FIN包回复确认包, 直到另一端也发送请求终止数据包FIN,另一端作出确认应答,并且还要发送一个ACK=1表示确认号有效,如果没有收到这个就可以进行重传.

ca1m4n的博客 1万+

Latex里面关于作者名字显示有灰色; 二级+三级标题斜体;

Latex里面关于 作者名字显示有灰色; 二级+三级标题斜体,与正文的距离; 邮箱字体;ORCID字体设置(整理不易,请先关注,Thanks)

m0_61015717的博客 746

CodeIgniter框架源码笔记(6)——支持友好的URI地址:路由类Router.php

Router类:将URI映射到对应的控制器及方法 Router类大量代码处理的是自定义路由,该类要支撑以下几个功能点: 1、自定义路由规则 在 application/config/routes.php 文件中的 $route 的数组,利用它可以设置路由规则。 在路由规则中可以使用通配符或正则表达式。 使用通配符:$route['product/:num'] = 'catalog/prod

程序老兵的博客 5156

Pytest教程:Pytest插件原理

Pytest的插件系统是其强大功能的核心,它允许用户和开发者扩展和定制测试框架的行为。要深入理解Pytest插件的原理,我们需要从Pytest的架构和设计模式开始,然后深入到底层源码。

旦莫的博客 637

CodeIgniter源码阅读(一)(index.php && CodeIgniter.php)

菜鸟看框架,一些不懂的地方是网上查的或者看别人写的源码阅读里的分析~如有雷同还望海涵:)。所有写下来的文字都是自己看懂和理解了之后的~如有错误还望指出,我是个新人~~~ ####index.php 设置项目运行环境; 设置系统目录; define一些常量; BASEPATH -...

chengjuanfu9099的博客 1万+

基于3D相机的人体穴位识别与标注系统——完整Python实现方案

摘要 本文提出了一种基于3D相机的人体穴位识别与标注系统完整实现方案。系统采用Intel RealSense D435相机实时采集RGB-D数据,结合MediaPipe Pose模型进行人体33个关键骨骼点的三维姿态估计。通过建立穴位与骨骼关键点的映射关系,利用相机内参和深度信息将二维图像坐标转换为三维空间坐标。系统采用模块化设计,包含数据采集层、算法处理层、坐标转换层和可视化层,实现了穴位定位、三维坐标转换和点云可视化功能。关键技术包括:基于骨度折量法的穴位映射算法、深度图像到三维点云的转换方法,以及Op

AI的内部世界的博客 273

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

php的引导文件,CI框架源码阅读笔记4 引导文件CodeIgniter.php

到了这里,终于进入CI框架的核心了。既然是“引导”文件,那么就是对用户的请求、参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位。例如,用户的请求url:http://you.host.com/usr/reg经过引导文件,实际上会交给Application中的UsrController控制器的reg方法去处理。 这之中,CodeIgniter.php做了哪些工作?我们一步步来看。1. ...

weixin_32642653的博客 259

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

CI框架源码解析二之引导文件CodeIgniter.php

上篇解析入口文件写到载入 core/CodeIgniter.php框架核心文件,启动框架。CodeIgniter.php文件被称为BOOTSTRAP,也就是引导文件,这里也就是CI框架的核心了。其实把CodeIgniter.php这个文件的代码运行一次,就是整个CI应用都完成了一次完整的运作流程了。其中会加载一些组件,引入很多外部文件,等等。所以建议在阅读此文件代码的时候,第一遍先阅读它的大概流程

让编程改变世界 4446

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

CI框架 router.php,CI框架源码翻阅-Router.php

CI框架源码阅读---------Router.phpconfig =& load_class('Config', 'core');$this->uri =& load_class('URI', 'core');log_message('debug', "Router Class Initialized");}// ------------------------------...

weixin_30694389的博客 143

CI框架源码完全分析之核心文件Codeigniter.php

/**  * 转载请保留链接  * @link http://www.phpddt.com  */ //如果入口文件系统目录常量BASEPATH没定义,就挂了 if ( ! defined('BASEPATH')) exit('No direct script access allowed');         //定义常量:CI_VERSION,CI_CORE define('CI

txj236的专栏 913

CI框架源码阅读笔记4 引导文件CodeIgniter.php

  到了这里,终于进入CI框架的核心了。既然是“引导”文件,那么就是对用户的请求、参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位。例如,用户的请求url: http://you.host.com/usr/reg 经过引导文件,实际上会交给Application中的UsrController控制器的reg方法去处理。 这之中,CodeIgniter.php做了哪些工作?我们...

weixin_30780221的博客 129

CodeIgniter源码分析之Router.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // ------------------------------------------------------------------------ /** * Router Class */ class CI_Router { /*

Dosice 854

CI3设置子目录控制器为默认控制器的解决办法

在框架中配置文件多目录、前后台应该是个很常见的事情。像一般的php框架(CI、Tp等)采用都是单一入口模式,或许有人会直接在框架根目录新建文件admin.php,然后改变框架app结构,以达到访问不同入口文件名获得不同资源的效果。那么在CI中一样可以这样做,不过个人觉得这种方法太浪费资源(占用了几十k的资源吧)。于是在‘求学问...

weixin_33834679的博客 517

codeigniter源代码分析之路由处理类 Router.php

Router 与 URI 类联系紧密 CodeIgniter.php 里面调用的是 Router 的

fanyilong的专栏 2212

CodeIgniter框架源码学习之路由类--Router.php

文件位置:./system/core/Router.php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014 -

落地窗前梦残夜 867

CI框架教程4——将默认路由放到子文件下

Codeigniter框架在版本3.0以前还可以将默认路由地址设置到子文件下中,在3.0以后就不可以了,本篇博文内容就是基于这一问题提出的解决方案。比如你想设置的默认路由地址application文件夹下controller文件夹下的home模块中(在路由配置文件中这样写): $route['default_controller'] = 'home/index';          这

让编程改变世界 1540

CodeIgniter使用总结

最近在使用CodeIgniter开发一个企业站项目,一开始是把前台和后台分开作为两个单独的项目,但后来想想为啥不能放在一个项目里呢,便有了改造的想法。 先上一张最终的目录结构图 application/ |-- config/ | |-- routes.php |-- controllers/ | |-- ...

weixin_34082854的博客 170
上一篇: CodeIgniter 核心代码阅读-URI解析文件URI.php
下一篇: CodeIgniter 核心代码阅读-输出文件Output.php
zhxp_870516
博客等级 码龄19年 28粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值