简单实现php框架

原创 2019-10-07 20:55:57 334
摘要://config.php<?php return ['app'=>['debug'=>true],//默认配置模块、控制器、方法 'route'=>['module'=>'admin','controller'=>'index','acti

//config.php

<?php

return [

'app'=>['debug'=>true],

//默认配置模块、控制器、方法

'route'=>['module'=>'admin','controller'=>'index','action'=>'index']

];

// admin\controller\index.php

<?php

namespace admin\controller;


class Index{

         public function Index($name='alice'){

                        return "my name is {$name}.";

        }

}



//route.php

<?php

class Route{

             //路由配置参数

             protected $route=[];

             //Url

             protected $pathinfo=[];

            //参数

             protected $params=[];

             public function __construct($route){

                     //加载配置信息

                     $this->route = $route;

             }

            // 获取配置信息

            public function getVar(){

                        return $this->route;

             }

            //url参数解析 

            public function parse($qstr){

                    //url转为数组

                    $arr = explode('/',trim($qstr,'/'));

                    $arr = array_filter($arr);

                    

                    switch(count($arr)){

                         case 0 :

                         $this->pathinfo = $this->route;

                         break;

                         case 1 :

                                 $this->pathinfo['module'] = $arr[0];

                                 $this->pathinfo['controller'] = $this->route['controller'];

                                 $this->pathinfo['action'] = $this->route['action'];

                                 break;

                        

                         case 2 :

                                 $this->pathinfo['module'] = $arr[0];

                                 $this->pathinfo['controller'] = $arr[1];

                                 $this->pathinfo['action'] = $this->route['action'];

                                 break;

                        

                         case 3 :

                                 $this->pathinfo['module'] = $arr[0];

                                 $this->pathinfo['controller'] = $arr[1];

                                 $this->pathinfo['action'] = $arr[2];

                                 break;

                        

                         default :

                                 $this->pathinfo['module'] = $arr[0];

                                 $this->pathinfo['controller'] = $arr[1];

                                 $this->pathinfo['action'] = $arr[2];

                                 $paramVal = array_slice($arr,3);

                                 for($i=0;$i<count($paramVal);$i+=2){

                                                 if(isset($paramVal[$i+1])){

                                                         $this->params[$paramVal[$i]] = $paramVal[$i+1];

                                                         }

                                         }

                                         break;

                                 }

                         return $this->params;

             }

            

             public function dispatch(){

                         //获取模块名

                         $module = $this->pathinfo['module'];

                         //获取控制器类

                         $controller = $module.'\\controller\\'.ucfirst($this->pathinfo['controller']);

                         //获取方法名

                         $action = $this->pathinfo['action'];

                         //判断类中是否上辈子在此方法

                         if(!method_exists($controller,$action)){

                                      echo '不存在此方法!';

                           }

                         //调用call_user_func_array

                         return call_user_func_array([new $controller,$action],$this->params);

                }

}


//加载默认配置

$route = (require 'config.php')['route'];

//声明Route类实例

$route = new Route($route);

//获取url参数

$qstr = $_SERVER['QUERY_STRING'];


//echo '<pre>'.var_export($route->getVar(),true).'<br>';


//echo $qstr;

//参数路由解析

$route->parse($qstr);


//echo '<pre>'.var_export($arr,true).'<br>';

//加载类文件

require __DIR__.'/admin/controller/Index.php';

//请求分发

echo  $route->dispatch();


批改老师:查无此人批改时间:2019-10-12 16:28:07
老师总结:完成的不错。自己写路由,可以更好的了解路由机制。继续加油

发布手记

热门词条