LARAVEL 路由原理分析

<?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');



 ?>

原文地址:https://www.cnblogs.com/handongyu/p/8867342.html