Returning Responses =================== HTTP周期的一部分工作是将用户请求的内容æ£ç¡®è¿”回给用户。Phalcon框架使用组件 :doc:`Phalcon\\HTTP\\Response <../api/Phalcon_Http_Response>` æ¥å®žçŽ°è¿™ä¸ªä»»åŠ¡ã€‚HTTPå“应(HTTP responses)通常由头部信æ¯åŠç½‘页主体组æˆã€‚䏋颿˜¯åŸºæœ¬çš„ä½¿ç”¨è¯æ³•: .. code-block:: php <?php // Getting a request instance $request = new \Phalcon\Http\Request(); //Set status code $response->setRawHeader(404, "Not Found"); //Set the content of the response $response->setContent("Sorry, the page doesn't exist"); //Send response to the client $response->send(); å‘é€å¤´éƒ¨ä¿¡æ¯(Working with Headers) -------------------------------------------- 头部信æ¯(Headers)是整个HTTPå“应ä¸çš„é‡è¦ç»„æˆéƒ¨åˆ†ã€‚它包括å“应状æ€ï¼Œå¦‚HTTP状æ€ï¼Œå“应的类型ç‰éžå¸¸æœ‰ç”¨çš„ä¿¡æ¯ã€‚ ä½ å¯ä»¥é€šè¿‡ä»¥ä¸‹æ–¹æ³•设置头部信æ¯ï¼š .. code-block:: php <?php //Setting it 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"); HTTP头部信æ¯ç”± :doc:`Phalcon\\HTTP\\Response\\Headers <../api/Phalcon_Http_Response_Headers>` 管ç†ï¼Œè¿™ä¸ªç±»å…许在å‘客户端å‘回数æ®å‰ï¼Œå‘客户端å‘é€HTTP头部信æ¯ï¼š .. code-block:: php <?php //Get the headers bag $headers = $response->getHeaders(); //Get a header by its name $contentType = $response->getHeaders()->get("Content-Type"); 使用é‡å®šå‘(Making Redirections) --------------------------------------- 使用 :doc:`Phalcon\\HTTP\\Response <../api/Phalcon_Http_Response>` ï¼Œä½ å¯ä»¥å®žçްHTTPé‡å®šå‘: .. code-block:: php <?php //Making a redirection using the local base uri $response->redirect("posts/index"); //Making a redirection to an external URL $response->redirect("http://en.wikipedia.org", true); //Making a redirection specifyng the HTTP status code $response->redirect("http://www.example.com/new-location", true, 301); 所有由 "url"æœåŠ¡(by default :doc:`Phalcon\\Mvc\\Url <url>`)äº§ç”Ÿçš„å†…éƒ¨è¿žæŽ¥ï¼Œä½ å¯ä»¥åœ¨ç¨‹åºä¸è¿™æ ·ä½¿ç”¨é‡å®šå‘到其他路由上: .. code-block:: php <?php //Making a redirection based on a named route $response->redirect(array( "for" => "index-lang", "lang" => "jp", "controller" => "index" )); éœ€è¦æ³¨æ„的是,é‡å®šå‘ä¸ä¼šç¦ç”¨è§†å›¾ç»„ä»¶ã€‚å› æ¤ï¼Œå¦‚æžœä½ æƒ³ä»Žä¸€ä¸ªcontroller/actioné‡å®šå‘到å¦ä¸€ä¸ªcontroller/acton上,视图将æ£å¸¸æ˜¾ç¤ºã€‚å½“ç„¶ï¼Œä½ ä¹Ÿå¯ä»¥ä½¿ç”¨ $this->view->disable() ç¦ç”¨è§†å›¾è¾“出。