目录
为什么要开发自己的框架?
- 为了更快的开发效率
- 为了更高的运行效率
- 为了更好地证明自己(装x)
你需要什么?
- 能够熟练地使用使用php
- 较为深刻的理解面向对象和mvc概念
- 较强的逻辑能力和动手能力
我的项目 github地址
https://github.com/cjk134796/cjk_imooc
2-3 路由类
目的
: 通过地址栏的参数来找到对应的控制器和方法
本质
:
- 隐藏index.php
- 获取uRL 参数部分
- 返回对应的控制器和方法
- url多余部分转换成 GET ;例如 /id/1/str/2/test/3
<?php
namespace core\lib;
use core\lib\conf;
class route{
public $ctrl;
public $action;
public function __construct()
{
/**
* 1.隐藏index.php
* 2.获取uRL 参数部分
* 3.返回对应的控制器和方法
*/
if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'){
$path = $_SERVER['REQUEST_URI'];
$patharr = explode('/',trim($path,'/'));
if(isset($patharr[0])){
$this->ctrl = $patharr[0];
}
unset($patharr[0]);
if(isset($patharr[1])){
$this->action = $patharr[1];
unset($patharr[1]);
}else{
$this->action = conf::get('ACTION','route'); //更换成配置项
}
//url多余部分转换成 GET
$_GET = [];
$count = count($patharr) + 2;
$i = 2;
while ($i < $count){
if(isset($patharr[$i + 1])){
$_GET[$patharr[$i]] = $patharr[$i + 1];
}
$i = $i + 2;
}
}else{
$this->ctrl = conf::get('CTRL','route');
$this->action = conf::get('ACTION','route');
}
}
}
2-4 加载控制器
目的
:mvc结构的v,解耦,实现业务逻辑
本质
:
- 通过路由类获取控制器和方法名
- 如果存在文件 则 实例化对应类和使用对应方法,反之
<?php
namespace app\ctrl;
use core\lib\conf;
class indexCtrl extends \core\imooc
{
public function index()
{
$temp