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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 中間件 你可以在你的 Slim 應用_之前(before)_ 和 _之后(after)_ 運行代碼來處理你認為合適的請求和響應對象。這就叫做_中間件_。為什么要這么做呢?比如你想保護你的應用不遭受跨站請求偽造。也許你想在應用程序運行前驗證請求。中間件對這些場景的處理簡直完美。 ## 什么是中間件? 從技術上來講,中間件是一個接收三個參數的_可回調(callable)_對象: 1. `\Psr\Http\Message\ServerRequestInterface` - PSR7 請求對象 2. `\Psr\Http\Message\ResponseInterface` - PSR7 響應對象 3. `callable` - 下一層中間件的回調對象 它可以做任何與這些對象相應的事情。唯一硬性要求就是中間件**必須**返回一個 `\Psr\Http\Message\ResponseInterface` 的實例。 每個中間件都 **應當**調用下一層中間件,并講請求和響應對象作為參數傳遞給它(下一層中間件)。 ## 中間件是如何工作的? 不同的框架使用中間件的方式不同。在 Slim 框架中,中間件層以同心圓的方式包裹著核心應用。由于新增的中間件層總會包裹所有已經存在的中間件層。當添加更多的中間件層時同心圓結構會不斷的向外擴展。 當 Slim 應用運行時,請求對象和響應對象從外到內穿過中間件結構。它們首先進入最外層的中間件,然后然后進入下一層,(以此類推)。直到最后它們到達了 Slim 應用程序自身。在 Slim 應用分派了對應的路由后,作為結果的響應對象離開 Slim 應用,然后從內到外穿過中間件結構。最終,最后出來的響應對象離開了最外層的中間件,被序列化為一個原始的 HTTP 響應消息,并返回給 HTTP 客戶端。下圖清楚的說明了中間件的工作流程: ![Middleware architecture](https://box.kancloud.cn/2015-10-23_5629d1b6da0db.png) ## 如何編寫中間件? 中間件是一個接收三個參數的可回調(callable)對象,三個參數是:請求對象、響應對象以及下一層中間件。中間件**必須**返回一個 `\Psr\Http\Message\ResponseInterface` 的實例。 ### 閉包中間件示例。 這個示例中間件是一個閉包。 ``` <?php /** * Example middleware closure * * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request * @param \Psr\Http\Message\ResponseInterface $response PSR7 response * @param callable $next Next middleware * * @return \Psr\Http\Message\ResponseInterface */ function ($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; }; ``` ### 可調用類的中間件示例。 這個示例中間件是一個實現了 `__invoke()` 魔術方法的可調用類。 ``` <?php class ExampleMiddleware { /** * Example middleware invokable class * * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request * @param \Psr\Http\Message\ResponseInterface $response PSR7 response * @param callable $next Next middleware * * @return \Psr\Http\Message\ResponseInterface */ public function __invoke($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; } } ``` 要使用這個類作為中間件,你可以使用 `-&gt;add( new ExampleMiddleware() );` 函數連接在 `$app`, `Route`, 或 `group()` 之后,在下面的代碼中,它們中的任意一個都可以表示 $subject。 ``` $subject->add( new ExampleMiddleware() ); ``` ## 如何添加中間件? 你可以在 Slim 應用中添加中間件,或者添加到一個單獨的 Slim 應用路由,或者是路由組。所有場景都能接受相同的中間件和實現相同的中間件接口。 ### 應用程序中間件 應用程序中間件被每個_傳入(incoming)_ HTTP 請求調用。應用中間件可以通過 Slim 應用實例的 `add()` 方法來添加。下面這是一個添加上面那個閉包中間件的例子: ``` <?php $app = new \Slim\App(); $app->add(function ($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; }); $app->get('/', function ($req, $res, $args) { echo ' Hello '; }); $app->run(); ``` 輸出的 HTTP 響應主體為: ``` BEFORE Hello AFTER ``` ### 路由中間件 路由中間件_只有_在當前 HTTP 請求的方法和 URI 都與中間件所在路由相匹配時才會被調用。路由中間件會在你調用了任意 Slim 應用的路由方法(e.g., `get()` 或 `post()`)后被立即指定。 每個路由方法返回一個 `\Slim\Route` 的實例,這個類提供了相同的中間件接口作為 Slim 應用的實例。使用路由實例的 `add()` 方法將中間件添加到路由中。下面這是一個添加上面那個閉包中間件的例子: ``` <?php $app = new \Slim\App(); $mw = function ($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; }; $app->get('/', function ($req, $res, $args) { echo ' Hello '; })->add($mw); $app->run(); ``` 輸出的 HTTP 響應主體為: ``` BEFORE Hello AFTER ``` ### Group Middleware 除了整個應用,以及標準的路由可用接收中間件,還有 `group()` 多路由定義功能同樣允許在其內部存在獨立的路由。路由組中間件_只有_在其路由與已定義的 HTTP 請求和 URI 中的一個相匹配時才會被調用。要在回調中添加中間件,以及整組中間件,通過在 `group()` 方法后面連接 `add()` 來設置。 下面的示例程序,在一組 URL 處理程序(url-handlers)中使用了回調中間件。 ``` <?php require_once __DIR__.'/vendor/autoload.php'; $app = new \Slim\App(); $app->get('/', function ($request, $response) { return $response->getBody()->write('Hello World'); }); $app->group('/utils', function () use ($app) { $app->get('/date', function ($request, $response) { return $response->getBody()->write(date('Y-m-d H:i:s')); }); $app->get('/time', function ($request, $response) { return $response->getBody()->write(time()); }); })->add(function ($request, $response, $next) { $response->getBody()->write('It is now '); $response = $next($request, $response); $response->getBody()->write('. Enjoy!'); return $response; }); ``` 在調用 `/utils/date` 方法時,將輸出類似下面這樣子的字符串: ``` It is now 2015-07-06 03:11:01\. Enjoy! ``` 訪問 `/utils/time` 將會輸出類似下面這樣子的字符串: ``` It is now 1436148762\. Enjoy! ``` 但訪問 `/` _域名根目錄(domain-root)_時,將會有如下輸出,因為并沒有分配中間件。 ``` Hello World ```
                  <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>

                              哎呀哎呀视频在线观看