php注解使用示例( 三 )

route.php <?phpclass app{public function appInit():self{if(glob("./controller/*.php")){foreach(glob("./controller/*.php") as $fileName){require_once($fileName);$className = str_replace("./controller/","",str_replace(".php","",$fileName));route::setRoute($className);//route::$all;exit();}}return $this;}public function run($config){$controller = $_GET['c']??$config['default_controller'];$action = $_GET['a']??$config['default_function'];if(isset(route::$all[$controller][$action])){$route = route::$all[$controller][$action];$className = $route->controller;$function = $route->function;(new $className)->$function();}else{exit("404 Not Found!");}}}init.php <?phpclass indexController{#[route('/controller/index','get')]public function index():void{echo "This is attribute index controller \r\n";}#[route('/controller/test','get')]public function test():void{echo "This is attribute test controller \r\n";}}indexController.php <?phpclass workController{#[route('/controller/work','post')]public function work():void{echo "This is attribute work controller \r\n";}}workController.php <?php$config = array('default_controller'=>'index','default_function'=>'index',);return $config;config.php <?phprequire_once('./config.php');require_once('./route.php');require_once('./init.php');(new app)->appInit()->run($config);index.php

经验总结扩展阅读