찾다
백엔드 개발PHP 튜토리얼 php url伪静态弥合析
php url伪静态弥合析 Jun 13, 2016 pm 01:08 PM
actioncontrollerpathrouterstrpos

php url伪静态化解析

1、大家来说说URL映射吧
? ?一般url映射有两种方式,一种是通过mod_rewrite实现,这种网上教材很多我也不多说了。另外一种是在程序中模拟,比如类似zend Framework中的那种方式/index.php/controller/action/var1/value1/var2/value2/。这里方 式其实最主要是通过一个统一的输入接口,然后对url进行解析,最后转发到相应的controller中的module。

? ? 我这里写了两个简单函数来模拟。
? ? 第一个函数主要是进行地址解析,把类似/index.php/controller/action/var1/value1/var2/value2/的地址解析出来,一般来说要解析成三部分:controller,module,params。

<?php /**
 *对url路由进行简单的解析,支持对/path/to/site/index.php/module/action/parm/value
 * /path/to/site/index.php?/module/action/parm/value和
 * /path/to/site/?/module/action/parm/value三种形式的处理
 *@param:null
 *@return:router array
 */
function url_router() {
	$path = strip_tags ( $_SERVER ['REQUEST_URI'] );
	$strpos = strpos ( $path, '.php' );
	if ($strpos) {
		$path = substr ( $path, $strpos + 4 );
	} else {
		if (empty ( $_SERVER ['QUERY_STRING'] )) {
			$strpos = strpos ( $path, '?' );
			if ($strpos) {
				$path = substr ( $path, $strpos + 1 );
			} else {
				$path = '';
			}
		} else {
			$path = $_SERVER ['QUERY_STRING'];
		}
	}
	//统一化$path的格式,如果$path的第一个字符为/则去掉
	if ($path [0] == '/') {
		$path = substr ( $path, 1 );
	}
	//解析,并且路由
	if (! empty ( $path )) {
		$path = explode ( '/', $path );
		$router ['controller'] = $path [0];
		$router ['action'] = (! empty ( $path [1] )) ? $path [1] : 'index';
		//print_r($path);
		for($i = 2; $i < sizeof ( $path ); $i = $i + 2) {
			$params [$path [$i]] = (isset ( $path [$i + 1] )) ? $path [$i + 1] : '';
		}
		$router ['params'] = $params;
	} else {
		//默认路由信息
		$router ['controller'] = 'index';
		$router ['action'] = 'index';
		$router ['params'] = array ();
	}
	return $router;
}
?> 

这里就完成主要的url解析功能,然后是转发映射,下面这个函数实现(注意这个函数的实现是结合了我自己的架构,所以你采用的话需要相应的修改,当然你的MVC如果类似zend Framework,那应该要该的不多。)

<?php function url_dispatch($router, $app_path = '/app/controllers/') {
	require_once (SERVER_PATH . '/libs/controller.class.php');
	$controller = $router ['controller'] . 'Controller';
	//echo SERVER_PATH.$app_path.$controller.'.class.php';
	if (! file_exists ( SERVER_PATH . $app_path . $controller . '.class.php' ))
		die ( '缺少必要的类!' );
	require_once (SERVER_PATH . $app_path . $controller . '.class.php');
	$controller = new $controller ();
	$controller->_setParam ( $router ['params'] );
	$controller->{$router ['action'] . 'Action'} ();
	return true;
}
?> 
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
SpringBoot扫描不到Controller怎么解决SpringBoot扫描不到Controller怎么解决May 14, 2023 am 08:10 AM

SpringBoot小白创建项目,扫描不到Controller一系列问题1.2.3.4.5.6.还有一种办法是在启动服务类的入门,添加@ComponentScan(basePackages={“xxx.xxx.xx”,“xxx.xxx.xx”})里面的是包的全限定名,可以为多个SpringBoot自定义controller无法扫描到SpringBoot自定义controller路由找不到,原因是启动类和自定义的Controller包不在同一级目录下。官方建议application.java放的位

设置Linux系统的PATH环境变量步骤设置Linux系统的PATH环境变量步骤Feb 18, 2024 pm 05:40 PM

Linux系统如何设置PATH环境变量在Linux系统中,PATH环境变量用于指定系统在命令行中搜索可执行文件的路径。正确设置PATH环境变量可以方便我们在任何位置执行系统命令和自定义命令。本文将介绍如何在Linux系统中设置PATH环境变量,并提供详细的代码示例。查看当前的PATH环境变量在终端中执行以下命令,可以查看当前的PATH环境变量:echo$P

React Router使用指南:如何实现前端路由控制React Router使用指南:如何实现前端路由控制Sep 29, 2023 pm 05:45 PM

ReactRouter使用指南:如何实现前端路由控制随着单页应用的流行,前端路由成为了一个不可忽视的重要部分。ReactRouter作为React生态系统中最受欢迎的路由库,提供了丰富的功能和易用的API,使得前端路由的实现变得非常简单和灵活。本文将介绍ReactRouter的使用方法,并提供一些具体的代码示例。安装ReactRouter首先,我们需

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 BlackDJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 BlackSep 04, 2024 am 06:51 AM

DJI has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

SpringBoot多controller如何添加URL前缀SpringBoot多controller如何添加URL前缀May 12, 2023 pm 06:37 PM

前言在某些情况下,服务的controller中前缀是一致的,例如所有URL的前缀都为/context-path/api/v1,需要为某些URL添加统一的前缀。能想到的处理办法为修改服务的context-path,在context-path中添加api/v1,这样修改全局的前缀能够解决上面的问题,但存在弊端,如果URL存在多个前缀,例如有些URL需要前缀为api/v2,就无法区分了,如果服务中的一些静态资源不想添加api/v1,也无法区分。下面通过自定义注解的方式实现某些URL前缀的统一添加。一、

如何设置path环境变量如何设置path环境变量Sep 04, 2023 am 11:53 AM

设置path环境变量的方法:1、Windows系统,打开“系统属性”,点击“属性”选项,点击“高级系统设置”,在“系统属性”窗口中,选择“高级”标签,然后点击“环境变量”按钮,找到并点击“Path”编辑保存后即可;2、Linux系统,打开终端,打开你的bash配置文件,在文件末尾添加“export PATH=$PATH:文件路径”保存即可;3、MacOS系统,操作同上。

如何正确设置Linux中的PATH环境变量如何正确设置Linux中的PATH环境变量Feb 22, 2024 pm 08:57 PM

如何正确设置Linux中的PATH环境变量在Linux操作系统中,环境变量是用来存储系统级别的配置信息的重要机制之一。其中,PATH环境变量被用来指定系统在哪些目录中查找可执行文件。正确设置PATH环境变量是确保系统正常运行的关键一步。本文将介绍如何正确设置Linux中的PATH环境变量,并提供具体的代码示例。1.查看当前PATH环境变量在终端中输入以下命

Linux中PATH环境变量的作用和重要性Linux中PATH环境变量的作用和重要性Feb 21, 2024 pm 02:09 PM

《Linux中PATH环境变量的作用和重要性》PATH环境变量是Linux系统中非常重要的环境变量之一,它定义了系统在哪些目录中寻找可执行程序。在Linux系统中,当用户在终端输入一个命令时,系统会在PATH环境变量所列出的目录中逐个查找是否存在该命令的可执行文件,如果找到则执行,否则会提示“commandnotfound”。PATH环境变量的作用:简化

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구