<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之旅 廣告
                [TOC] # 返回響應 HTTP周期的一部分是返回對客戶端的響應。`Phalcon\Http\Response`是為實現此任務而設計的Phalcon組件。HTTP響應通常由標題和正文組成。以下是基本用法的示例: ```php <?php use Phalcon\Http\Response; // Getting a response instance $response = new Response(); // Set status code $response->setStatusCode(404, 'Not Found'); // Set the content of the response $response->setContent("Sorry, the page doesn't exist"); // Send response to the client $response->send(); ``` 如果您使用完整的MVC堆棧,則無需手動創建響應。但是,如果您需要直接從控制器的操作返回響應,請遵循以下示例: ```php <?php use Phalcon\Http\Response; use Phalcon\Mvc\Controller; class FeedController extends Controller { public function getAction() { // Getting a response instance $response = new Response(); $feed = // ... Load here the feed // Set the content of the response $response->setContent( $feed->asString() ); // Return the response return $response; } } ``` ## 使用Headers Headers是HTTP響應的重要部分。它包含有關響應狀態的有用信息,如HTTP狀態,響應類型等等。 您可以通過以下方式設置headers : ```php <?php // Setting a header by its name $response->setHeader('Content-Type', 'application/pdf'); $response->setHeader('Content-Disposition', "attachment; filename='downloaded.pdf'"); // Setting a raw header $response->setRawHeader('HTTP/1.1 200 OK'); ``` `Phalcon\Http\Response\Headers` 包內部管理標頭。 此類在將標頭發送到客戶端之前檢索標頭: ```php <?php // Get the headers bag $headers = $response->getHeaders(); // Get a header by its name $contentType = $headers->get('Content-Type'); ``` ## 執行重定向 使用`Phalcon\Http\Response`,您還可以執行HTTP重定向: ```php <?php // Redirect to the default URI $response->redirect(); // Redirect to the local base URI $response->redirect('posts/index'); // Redirect to an external URL $response->redirect('http://en.wikipedia.org', true); // Redirect specifying the HTTP status code $response->redirect('http://www.example.com/new-location', true, 301); ``` 所有內部URI都是使用`url`服務生成的(默認為`Phalcon\Mvc\Url`)。此示例演示了如何使用您在應用程序中定義的路由重定向: ```php <?php // Redirect based on a named route return $response->redirect( [ 'for' => 'index-lang', 'lang' => 'jp', 'controller' => 'index', ] ); ``` 請注意,重定向不會禁用視圖組件,因此如果存在與當前操作關聯的視圖,則無論如何都將執行該視圖。您可以通過執行`$this->view->disable()`來禁用控制器中的視圖。 ## HTTP緩存 提高應用程序性能和減少流量的最簡單方法之一是使用HTTP緩存。大多數現代瀏覽器都支持HTTP緩存,這也是許多網站目前速度很快的原因之一。 在第一次提供頁面時,應用程序發送的以下header值可以更改HTTP緩存: * **`Expires:`** 使用此標頭,應用程序可以設置將來或過去的日期,告訴瀏覽器何時頁面必須到期。 * **`Cache-Control:`** 此標頭允許指定頁面在瀏覽器中應被視為新鮮的時間。 * **`Last-Modified:`** 此標頭告訴瀏覽器最后一次更新網站,避免重新加載頁面。 * **`ETag:`** etag是必須創建的唯一標識符,包括當前頁面的修改時間戳。 ### 設置過期時間 過期時間是在客戶端(瀏覽器)中緩存頁面的最簡單且最有效的方法之一。從當前日期開始,我們添加頁面將存儲在瀏覽器緩存中的時間。在此日期到期之前,不會從服務器請求新內容: ```php <?php $expiryDate = new DateTime(); $expiryDate->modify('+2 months'); $response->setExpires($expiryDate); ``` 響應組件在Expires標頭中按預期自動顯示GMT時區中的日期。 如果我們將此值設置為過去的日期,則瀏覽器將始終刷新請求的頁面: ```php <?php $expiryDate = new DateTime(); $expiryDate->modify('-10 minutes'); $response->setExpires($expiryDate); ``` 瀏覽器依靠客戶端的時鐘來評估此日期是否已過。可以修改客戶端時鐘以使頁面過期,這可能代表對此高速緩存機制的限制。 ### Cache-Control 此標頭提供了一種更安全的方式來緩存所服務的頁面。我們必須指定一個時間(以秒為單位)告訴瀏覽器它必須將頁面保留在其緩存中多長時間: ```php <?php // Starting from now, cache the page for one day $response->setHeader('Cache-Control', 'max-age=86400'); ``` 以這種方式實現相反的效果(避免頁面緩存): ```php <?php // Never cache the served page $response->setHeader('Cache-Control', 'private, max-age=0, must-revalidate'); ``` ### E-Tag `entity-tag`或`E-tag`是一種唯一標識符,可幫助瀏覽器實現頁面在兩個請求之間是否發生了變化。必須計算標識符,并考慮到如果先前提供的內容已更改,則必須更改標識符: ```php <?php // Calculate the E-Tag based on the modification time of the latest news $mostRecentDate = News::maximum( [ 'column' => 'created_at' ] ); $eTag = md5($mostRecentDate); // Send an E-Tag header $response->setHeader('E-Tag', $eTag); ```
                  <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>

                              哎呀哎呀视频在线观看