框架原理之路由(三)
在core
目录下,新建一个名为lib
的子目录,然后把我们前面写个Route.php
这个文件移动到这个目录下。
因为route类文件路径修改,所以在实例化的时候:
new \core\lib\route();
然后我们来完善Route.php
:
<?php namespace core\lib; class Route { public $controller; // 控制器 public $action; // 方法(动作) public function __construct() { // xxx.com/index.php/index/index // xxx.com/index.php/index /* * 1.隐藏index.php * 2.获取URL 参数部分 * 3.返回对应控制器和方法 * */ if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'){ // 处理成这种格式:index/index $path = $_SERVER['REQUEST_URI']; $pathArr = explode('/',trim($path,'/')); if(isset($pathArr[0])){ $this->controller = $pathArr[0]; } unset($pathArr[0]); if(isset($pathArr[1])){ $this->action = $pathArr[1]; unset($pathArr[1]); }else{ $this->action = 'index'; } // url多余部分(参数部分)转换成 GET // id/1/str/2 $count = count($pathArr) + 2; $i = 2; while($i < $count){ if(isset($pathArr[$i + 1])){ $_GET[$pathArr[$i]] == $pathArr[$i + 1]; } $i = $i + 2; } p($_GET); // 打印GET }else{ $this->controller = 'index'; // 默认控制器 $this->action = 'index'; // 默认方法 } } }
Apache配置文件.htaccess
Options +FollowSymLinks IndexIgnore / RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
分类:
编程语言
标签:
MoonsbirdBlog
, PHP
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)