返回简单实现php......登陆

简单实现php框架

肖凌2019-10-07 20:55:5733

//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();


最新手记推荐

• 依赖注入小例子• 模板显示用户信息表,并分页显示• 模板继承,模仿php页面• 模拟用户登录验证的案例 依赖注入 Facade• 创建一个验证器, 用来对模型对应的表中字段进行验证处理

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网