<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>

                # 配置路由 [TOC] 當我們進入一條鏈接時,應用程序做了以下處理: 1. 路由尋找 URL 對應的控制器。 2. 控制臺將指定的方法進行操作。 ## ThinkPHP 的路由 打開 route/app.php 看到這條語句,將路由做如下修改: ```php title="route/app.php" Route::get('hello', 'index/hello'); ``` 以上命令的功能: * Route:: 注冊路由靜態方法 * get 請求方式 * 'hello' 請求 URL * 'index/hello' 響應默認控制器中 hello 方法 (`app/controller/Index.php/:function hello`) 打開上述控制器文件,找到 `hello` 方法并修改為: app/controller/Index.php/:function hello ```php title="app/controller/Index.php/:function hello" public function hello() { return 'hello world'; } ``` 打開瀏覽器,訪問 `http://127.0.0.1:8000/hello` 則會出現 `hello world`,一個簡單的 GET 路由就完成了。 ## 在路由中直接輸出 在閱讀本小節之前,首先要知道一個概念閉包: http://php.net/manual/zh/functions.anonymous.php 稍作了解之后,我們來學習在路由中使用閉包。 打開路由文件,將剛才的語句進行修改: route/app.php ```php title="route/app.php" Route::get('hello', function() { return 'hello closures'; }); ``` 這時候再刷新瀏覽器,輸出語句就變成了 `hello closures`。 這就是路由中閉包的基本用法。 ## 在控制器中響應 JSON 現在請刪除剛才的閉包語句,重新寫入: route/app.php ```php title="route/app.php" Route::get('hello', 'index/hello'); ``` 進入路由對應的控制器,修改代碼: app/controller/Index.php/:function hello ```php title="app/controller/Index.php/:function hello" public function hello() { $data = ['TP6-P01' => 'API Server']; return json($data); } ``` > return json(); 是一個助手函數,可以輸出一個 JSON 數據給客戶端。 此時再刷新頁面,就會發現輸出了 JSON 數據。 ## 為路由提供參數 我們在第一次對路由文件進行修改的時候,將 `Route::get('hello/:name', 'index/hello');` 變為了 `Route::get('hello', 'index/hello');`。 其中,我們刪除了 `:name` 這幾個字符,`:name` 就是本節提到的參數。 將剛才寫入的語句改為: route/app.php ```php title="route/app.php" Route::get('hello/:name', function ($name) { return 'Hello,' . $name; }); ``` `function($name)` 中的 `$name` 賦值為 `:name `。 如果我們進入 `http://127.0.0.1:8000/hello/param`,那么就會輸出 `hello param` ## 調試 API > 之后的章節我們都將使用 Postman 來調試 API。 打開之前安裝好的 Postman,點擊右邊的 + ,在輸入框內輸入 `http://127.0.0.1:8000/hello/param` 并點擊藍色的 `Send` 按鈕。 你會發現返回了一段 HTML 片段,因為在上面的代碼中,我們并沒有使用 `json()` 函數來返回一個 JSON 數據。 所以重新修改之前的語句: ```php title="route/app.php" Route::get('hello/:name', function ($name) { return json( [ 'Your name is' => $name ] ); }); ``` 此時再使用 Postman 調試,則會返回一個符合格式的 JSON 數據了。 ![](https://img.kancloud.cn/c2/55/c255fdc0263d3c2333c1f728dc0cb5a8_2090x1549.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>

                              哎呀哎呀视频在线观看