大佬教程收集整理的这篇文章主要介绍了框架原理之路由(三),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在core
目录下,新建一个名为lib
的子目录,然后把我们前面写个Route.PHP
这个文件移动到这个目录下。
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
以上是大佬教程为你收集整理的框架原理之路由(三)全部内容,希望文章能够帮你解决框架原理之路由(三)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。