<?php


class App {
    protected $routes = [];
    protected $responseStatus = '200 OK';
    protected $responseContentType = 'text/html';
    protected $responseBody = 'Laravel学院';

    public function addRoute($routePath, $routeCallback) {

        $this->routes[$routePath] = $routeCallback->bindTo($this, __CLASS__);
    }

    public function dispatch($currentPath) {
        foreach ($this->routes as $routePath => $callback) {
            if( $routePath === $currentPath) {
                $callback();
            }
        }
        header('HTTP/1.1 ' . $this->responseStatus);
        header('Content-Type: ' . $this->responseContentType);
        header('Content-Length: ' . mb_strlen($this->responseBody));
        echo $this->responseBody;
    }

}


$app = new App();
$app->addRoute('user/nonfu', function(){
    $this->responseContentType = 'application/json;charset=utf8';
    $this->responseBody = '{"name":"LaravelAcademy"}';
});
$app->dispatch('user/nonfu');



 ?>

posted on 2018-04-17 16:07  阳光小白  阅读(385)  评论(0编辑  收藏  举报