框架运行流程
1.入口文件
<?php
/**
* 入口文件
* 1.定义常量
* 2.加载函数库
* 3.启动框架
*/
define('IMOOC',realpath('./'));
define('CORE',IMOOC.'/core');
define('APP',IMOOC.'/app');
define('DEBUG',true);
if (DEBUG) {
ini_set('display_error','On');
} else {
ini_set('display_error','Off');
}
require IMOOC . '/' . 'core/imooc.php';
spl_autoload_register('\\core\\imooc::load');
\core\imooc::run();
2.自动加载类
<?php
namespace core;
class imooc
{
public static function run()
{
$route = new \core\lib\route();
var_dump($route);
}
public static function load($class)
{
//自动加载类
require IMOOC . '/' . $class . '.php';
}
}
3.路由类
<?php
namespace core\lib;
class route
{
private $ctrl = 'index';
private $action = 'index';
/**
* route constructor.
*/
public function __construct()
{
// 正常网址 xxx.com/index/index
// xxx.com/index.php/index/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->ctrl = $patharr[0];
} else {
$this->ctrl = 'index';
}
unset($patharr[0]);
if (isset($patharr[1])) {
$this->action = $patharr[1];
} else {
$this->action = 'index';
}
unset($patharr[1]);
// url 多于部分转换为参数
// index/index/id/1/test/3
$count = count($patharr);
var_dump($count);
$i = 2;
while ($i <= $count) {
//防止数组越界
if (!isset($patharr[$i + 1])) {
break;
}
$_GET[$patharr[$i]] = $patharr[$i + 1];
$i = $i + 2;
}
var_dump($_GET);
} else {
$this->ctrl = 'index';
$this->action = 'index';
}
}
}