首先重新规划一下文件布局创建core/lib将路由类文件route.php放到lib中(记得更改命名空间namespace core\lib;)
当访问网址为http://www.qcy.com/QiCai/index/index时实际访问为http://www.qcy.com/QiCai/index.php/Index/index
route.php
<?php
namespace core\lib;
use core\lib\conf;
/**
* 路由
*/
class route
{
public $ctrl;
public $action;
public function __construct()
{
/**
* 1、隐藏index.php
* 2、获取URL参数部分
* 3、返回对应控制器和方法
*/
// p($_SERVER);
if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/QiCai/'){
$path = $_SERVER['REQUEST_URI'];
$patharr = explode('/', trim($path,'/'));
// p($patharr);
//判断控制器
if(isset($patharr[1])){
$this->ctrl = $patharr[1];
}
unset($patharr[1]);
//判断方法
if(isset($patharr[2])){
$this->action = $patharr[2];
unset($patharr[2]);
}else{
$this->action = conf::get('ACTION','route');
}
// p($patharr);die;
//url多余部分转换成 GET 比如: id/1/str/2
$count = count($patharr) + 2;
$i = 3;
while($i < $count){
if(isset($patharr[$i + 1])){
$_GET[$patharr[$i]] = $patharr[$i + 1];
}
$i = $i + 2;
}
// p($_GET);
}else{
$this->ctrl = conf::get('CTRL','route');
$this->action = conf::get('ACTION','route');
}
}
}
在框架根目录中创建.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>