框架原理之路由(三)

core目录下,新建一个名为lib的子目录,然后把我们前面写个Route.php这个文件移动到这个目录下。

qq%e5%9b%be%e7%89%8720170523100112

因为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

 

posted @   xiaobingch  阅读(132)  评论(0编辑  收藏  举报
编辑推荐:
· 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)
点击右上角即可分享
微信分享提示