php如何实现下列url格式路由分发:
app目录
┡┈▓ App
┊ ┡┈▓ Config //配置文件目录
┊ ┡┈▓ Controller //控制器目录
┊ ┊ ┡┈▒ Index.php // 默认控制器
┊ ┟┈▓ Modle // 模型目录
┊ ┟┈▓ View // 视图目录
┊ ┟┈▓ Module //模块目录
┊ ┊ ┟┈▓ Admin
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View
┊ ┊ ┟┈▓ Member
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View
http://www.xxx.com/category/list/order/hot/
http://www.xxx.com/admin/category/edit/
http://www.xxx.com/member/user/id/3/
http://www.xxx.com/index.php?c=category&a=list&order=hot
http://www.xxx.com/index.php?m=admin&c=category&a=edit
http://www.xxx.com/index.php?m=member&c=user&id=3
第1个url为前端访问,category为控制器,list为category类下的方法
第2个后台,admin为模块名,category为控制器,edit为category类下的方法
现在路由分发要实现以上2种形式的url的,怎样做?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
单一路由规则,将请求url全部转发给index.php处理.
这种是单一入口的,也是最简单。
但一入口 比较通用的路由规则
如果客户不设置伪静态,默认URL为第2种形式.
如果客户设置使用伪静态,就分别生成Apache和Nginx的rewrite规则,让客户自已复制去配置.
规则可以这样..
这样,程序只需按照第2种的形式处理
$_SERVER['REQUEST_URI'].程序中的URL根据是否设置伪静态,来动态生成.所有请求(除静态文件请求和不存在的目录或文件)外,全部转发到index.php。
Dispatcher中先进行自定义Router检测,匹配Router则执行Router绑定操作。未匹配到Router规则,执行常规操作检测并执行。