1、入口文件。index.phpphp
<?php header('Content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC'); require_once(__DIR__.'/configs/Website.php'); require_once(__DIR__.'/frame/Common/Function.php'); require_once(__DIR__.'/frame/App.php'); spl_autoload_register('frame\App::autoload'); frame\App::run(); ?>
2、站點全局配置文件。configs/Website.phphtml
<?php define('BASEPATH',dirname(dirname(__FILE__))); define('APP',BASEPATH.'/app'); define('CTRL',BASEPATH.'/app/Controllers'); define('CTRLNAMESPACE','app\Controllers\\'); define('DEBUG',TRUE);
3、全局路由配置文件。configs/Routes.phpapp
<?php /* * 路由結構 * array( * '命名空間'=>array( * '路由別名'=>'控制器@方法' * ) * ) */ return array( 'Home'=>array( 'index-index'=>'IndexController@index', 'index/test'=>'IndexController@test', ), 'Admin'=>array( 'login'=>'IndexController@login' ), );
4、框架啓動文件。frame/App.php框架
<?php namespace frame; use frame\Libs\Route; class App { //加載控制器文件執行方法 static public function run() { $route = new Route(); $ctrl = $route->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; } } }
5、加載配置類。frame/Libs/Config.phpui
<?php namespace frame\Libs; class Config { static $configs = []; /* * $file : 文件名 * $name : 鍵名 * $flag : 獲取所有選項 */ static public function get($file,$name,$flag = false) { if(isset(self::$configs[$file])) { return $flag ? self::$configs[$file] : self::$configs[$file][$name]; } else { $filename = BASEPATH.'/configs/'.ucfirst($file).'.php'; if(is_file($filename)) { $config = require_once($filename); $flag ? self::$configs[$file] = $config : self::$configs[$file] = $config[$name]; return $flag ? $config : $config[$name]; } else { throw new \Exception($filename."配置文件不存在", 1); } } } }
6、路由類。frame/Libs/Route.phpthis
<?php namespace frame\Libs; use frame\Libs\Config; class Route { public $ctrl; public $action; public $namespace; public function __construct() { $uri = $_SERVER['REQUEST_URI']; if(isset($uri) && $uri != '/') { $path = explode('?', trim($uri,'/')); $isExists = $this->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; } }
7、默認控制器。app/Controllers/WelcomeController.phpspa
<?php namespace app\Controllers; class WelcomeController { public function index() { echo ("<h1>Hello,World</h1>歡迎使用PHP-FRAME框架"); } }
8、隱藏index.php文件。.htaccesscode
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> 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}] </IfModule>
9、本地域名訪問 http://localhost。
htm