基于PSR-0编码规范开发一套PHP-MVC框架(一)

一、入口文件。index.php

二、站点全局配置文件。configs/Website.php

三、全局路由配置文件。configs/Routes.php

array(
*           '路由别名'=>'控制器@方法'
*       )
*   )
*/
return array(

    'Home'=>array(
        'index-index'=>'IndexController@index',
        'index/test'=>'IndexController@test',
    ),

    'Admin'=>array(
        'login'=>'IndexController@login'
    ),
    
);

四、框架启动文件。frame/App.php

ctrl;
        $action = $route->action;
        $namespace = $route->namespace;
        if($namespace) {
            $ctrlFile = CTRL.'/'.$namespace.'/'.$ctrl.'.php' ;
            $ctrlObj = CTRLNAMESPACE.$namespace.'\\'.$ctrl;
        } else {
            $ctrlFile = CTRL.'/'.$ctrl.'Controller.php';
            $ctrlObj =  CTRLNAMESPACE.$ctrl.'Controller' ;
        }
        if(is_file($ctrlFile)) {
            require_once($ctrlFile);
            $obj = new $ctrlObj;
            if(method_exists($obj,$action)) {
                $obj->$action();
            } else {
                throw new \Exception($action."方法不存在", 1);
                
            }
        } else {
            throw new \Exception($ctrl."控制器不存在", 1);
        }
    }
    
    //自动加载类文件
    static public function autoload($class)
    {
        $file = BASEPATH.'/'.str_replace('\\', '/', $class).'.php';
        if(is_file($file)) {
            require_once($file);
        } else {
            return false;
        }
    }
}

五、加载配置类。frame/Libs/Config.php

六、路由类。frame/Libs/Route.php

checkRoute($path[0]);
            if(!isset($path[0]) || !$isExists) {
                throw new \Exception($path[0]."路由不存在", 1);            
            }
        } else {
            $this->ctrl = 'Welcome';
            $this->action = 'index';
        }
    }

    private function checkRoute($alias)
    {
        $routes = Config::get('Routes',null,true);
        foreach ($routes as $key => $value) {

            foreach ($value as $k => $v) {
                if($k == $alias) {
                    $c = explode('@', $v);
                    $this->ctrl = $c[0];
                    $this->action = $c[1];
                    $this->namespace = $key;
                    return true;
                } 
            }
        }
        return false;
    }
}

七、默认控制器。app/Controllers/WelcomeController.php

Hello,World欢迎使用PHP-FRAME框架");
    }
}

八、隐藏index.php文件。.htaccess



    Options -MultiViews


RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

九、本地域名访问 http://localhost
图片描述

你可能感兴趣的:(medoo,twig,psr-0,mvc,php)