<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 創建core/RouteCollection.php ```` <?php namespace core; use core\request\RequestInterface; Class RouteCollection { protected $routes = []; // 所有路由存放 protected $route_index = 0; // 當前訪問的路由 public function getRoutes() // 獲取所有路由 { return $this->routes; } public $currGroup = []; // 當前組 public function group($attributes = [],\Closure $callback) { $this->currGroup[] = $attributes; call_user_func($callback,$this); // $callback($this); 跟這個一樣的效果 // group的實現主要的這個$this 這個$this將當前狀態傳遞到了閉包 array_pop($this->currGroup); } // 增加/ 如: GETUSER 改成 GET/USER protected function addSlash(& $uri) { return $uri[0] == '/' ? : $uri = '/'.$uri; } // 增加路由 public function addRoute($method,$uri,$uses) { $prefix = ''; // 前綴 $middleware = []; // 中間件 $namespace = ''; // 命名空間 $this->addSlash($uri); foreach ($this->currGroup as $group){ $prefix .= $group['prefix'] ?? false; if( $prefix) // 如果有前綴 $this->addSlash($prefix); $middleware = $group['middleware'] ?? []; // 合并組中間件 $namespace .= $group['namespace'] ?? ''; // 拼接組的命名空間 } $method = strtoupper($method); // 請求方式 轉大寫 $uri = $prefix .$uri; $this->route_index = $method . $uri; // 路由索引 $this->routes[$this->route_index] = [ // 路由存儲結構 用 GET/USER 這種方式做索引 一次性就找到了 'method' => $method, // 請求類型 'uri' => $uri, // 請求url 'action' => [ 'uses' => $uses, 'middleware' => $middleware, 'namespace' => $namespace ] ]; } public function get($uri,$uses) { $this->addRoute('get',$uri,$uses); return $this; } public function post($uri,$uses) { $this->addRoute('post',$uri,$uses); return $this; } public function put($uri,$uess){} // 略寫 ... public function delete($uri,$uses){} public function middleware($class) { $this->routes[$this->route_index]['action']['middleware'][] = $class; return $this; } // 獲取當前訪問的路由 public function getCurrRoute() { $routes = $this->getRoutes(); $route_index = $this->route_index; if( isset( $routes[ $route_index])) return $routes[ $route_index]; $route_index .= '/'; if( isset( $routes[ $route_index])) return $routes[ $route_index]; return false; } // 更具request執行路由 public function dispatch(RequestInterface $request) { $method = $request->getMethod(); $uri = $request->getUri(); $this->route_index = $method . $uri; $route = $this->getCurrRoute(); if(! $route) // 找不到路由 return 404; $routerDispatch = $route['action']['uses']; return $routerDispatch(); } } ```` ## 綁定路由 編輯 `app.php` ![](https://img.kancloud.cn/1f/36/1f362c470402b9dbb537798e5d5dfc25_528x202.png) ## 加載路由文件 編輯 `app.php` 的 `boot`方法 ``` App::getContainer()->get('router')->group([ 'namespace' => 'App\\controller' ], function ($router){ require_once FRAME_BASE_PATH . '/routes/web.php'; // 因為是require 所以web.php有$router這個變量 }); App::getContainer()->get('router')->group([ 'namespace' => 'App\\controller', 'prefix' => 'api' ], function ($router){ require_once FRAME_BASE_PATH . '/routes/api.php'; }); ``` ![](https://img.kancloud.cn/8f/9f/8f9f6e2eb5d0c538bac1f22b4f94145f_947x394.png) ## 創建routes/web.php和routes/api.php 編輯 `routes/web.php`: ![](https://img.kancloud.cn/33/6f/336fdbbbad85d567797139589be80064_643x308.png) ## 編輯index.php ![](https://img.kancloud.cn/21/39/213999e21a389b4a7fc0708d74ad3cae_748x620.png) ``` App::getContainer()->get('response')->setContent( App::getContainer()->get('router')->dispatch( App::getContainer()->get(\core\request\RequestInterface::class) ) )->send(); ``` ## 運行 ![](https://img.kancloud.cn/bf/9f/bf9ffbb7abed96f7a82aa8c2df2ac2b1_519x283.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看