多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[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); ```