<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之旅 廣告
                # 響應 ### 基本回應 幾乎可以使用該頁面使用的PHP方法返回響應。這包括[布局執行生命周期](https://octobercms.com/docs/cms/layouts#layout-life-cycle)和[AJAX處理程序定義中](https://octobercms.com/docs/ajax/handlers)包含的所有CMS方法。 #### 從CMS方法返回字符串 從CMS頁面,布局或組件方法返回字符串將此時停止該過程并覆蓋默認行為,因此此處將顯示“ Hello World”字符串而不是顯示頁面。 ~~~ public function onStart() { return 'Hello World'; } ~~~ #### 從AJAX處理程序返回字符串 從AJAX處理程序返回字符串將使用默認鍵將該字符串添加到響應集合中`result`。要求的部分內容仍將包含在響應中。 ~~~ public function onDoSomething() { return 'Hello World'; // ['result' => 'Hello World'] } ~~~ #### 從路線返回字符串 從[路由定義](https://octobercms.com/docs/services/router)返回字符串將與CMS方法相同,并顯示該字符串作為響應。 ~~~ Route::get('/', function() { return 'Hello World'; }); ~~~ #### 創建自定義響應 對于更健壯的解決方案,返回一個`Response`對象,該對象提供了多種構建HTTP響應的方法。我們將在本文中進一步探討該主題。 ~~~ $contents = 'Page not found'; $statusCode = 404; return Response::make($contents, $statusCode); ~~~ ### [](https://octobercms.com/docs/services/response-view#attaching-headers-to-responses)將標題附加到響應 請記住,大多數響應方法都是可鏈接的,可以流暢地構建響應。例如,您可以使用該`header`方法向響應添加一系列標頭,然后再將其發送回用戶: ~~~ return Response::make($content) ->header('Content-Type', $type) ->header('X-Header-One', 'Header Value') ->header('X-Header-Two', 'Header Value'); ~~~ 一個實際的例子可能是返回XML響應: ~~~ return Response::make($xmlString)->header('Content-Type', 'text/xml'); ~~~ ### [](https://octobercms.com/docs/services/response-view#attaching-cookies-to-responses)在響應中附加Cookie 該`withCookie`方法使您可以輕松地將Cookie附加到響應中。例如,您可以使用withCookie方法來生成cookie并將其附加到響應實例: ~~~ return Response::make($content)->withCookie('name', 'value'); ~~~ 該`withCookie`方法接受其他可選參數,這些參數使您可以進一步自定義cookie的屬性: ~~~ ->withCookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly) ~~~ ### [](https://octobercms.com/docs/services/response-view#other-response-types)其他回應類型 的`Response`立面可被用來方便地生成其它類型的反應的實例。 ### [](https://octobercms.com/docs/services/response-view#view-responses)查看回應 如果需要訪問`Response`類方法,但想返回[視圖](https://octobercms.com/docs/services/response-view#views)作為響應內容,則可以使用該`Response::view`方法以方便使用: ~~~ return Response::view('acme.blog::hello')->header('Content-Type', $type); ~~~ ### [](https://octobercms.com/docs/services/response-view#json-responses)JSON回應 該`json`方法將自動將`Content-Type`標頭設置為application / json,并使用`json_encode`PHP函數將給定數組轉換為JSON: ~~~ return Response::json(['name' => 'Steve', 'state' => 'CA']); ~~~ 如果您想創建JSONP響應,則`json`除了可以使用方法之外`setCallback`: ~~~ return Response::json(['name' => 'Steve', 'state' => 'CA']) ->setCallback(Input::get('callback')); ~~~ ### [](https://octobercms.com/docs/services/response-view#file-downloads)檔案下載 該`download`方法可用于生成響應,該響應強制用戶的瀏覽器在給定路徑下下載文件。該`download`方法接受文件名作為該方法的第二個參數,它將確定下載文件的用戶看到的文件名。最后,您可以將HTTP標頭數組作為該方法的第三個參數傳遞: ~~~ return Response::download($pathToFile); return Response::download($pathToFile, $name, $headers); return Response::download($pathToFile)->deleteFileAfterSend(true); ~~~ > **注意:**管理文件下載的Symfony HttpFoundation要求下載的文件具有ASCII文件名。 ### [](https://octobercms.com/docs/services/response-view#redirects)重新導向 重定向響應通常是`Illuminate\Http\RedirectResponse`該類的實例,并且包含將用戶重定向到另一個URL所需的適當標頭。生成`RedirectResponse`實例的最簡單方法是`to`在`Redirect`外觀上使用該方法。 ~~~ return Redirect::to('user/login'); ~~~ ### [](https://octobercms.com/docs/services/response-view#redirect-flash-data)返回帶有Flash數據的重定向 重定向到新的URL并將[數據刷新到會話](https://octobercms.com/docs/services/session)通常是在同一時間完成的。因此,為方便起見,您可以`RedirectResponse`在單個方法鏈中創建實例并將數據閃存到會話: ~~~ return Redirect::to('user/login')->with('message', 'Login Failed'); ~~~ > **注意:**由于該`with`方法會將數據閃爍到會話,因此您可以使用典型`Session::get`方法來檢索數據。 #### [](https://octobercms.com/docs/services/response-view#redirecting-previous-url)重定向到上一個URL 您可能希望將用戶重定向到他們以前的位置,例如,在提交表單之后。您可以使用以下`back`方法進行操作: ~~~ return Redirect::back(); return Redirect::back()->withInput(); ~~~ #### [](https://octobercms.com/docs/services/response-view#redirecting-current-page)重定向到當前頁面 有時您只想刷新當前頁面,可以使用以下`refresh`方法: ~~~ return Redirect::refresh(); ~~~ ### [](https://octobercms.com/docs/services/response-view#response-macros)響應宏 如果要定義可在各種路由和控制器中重復使用的自定義響應,則可以使用以下`Response::macro`方法: ~~~ Response::macro('caps', function($value) { return Response::make(strtoupper($value)); }); ~~~ 該`macro`函數接受名稱作為其第一個參數,并接受Closure作為其第二個參數。在`Response`類上調用宏名稱時,將執行宏的閉包: ~~~ return Response::caps('foo'); ~~~ 您可以在[插件注冊文件](https://octobercms.com/docs/plugin/registration#registration-methods)的`boot`方法中定義宏。另外,插件可以在插件目錄中提供一個名為**init.php**的文件,您可以用來放置宏注冊。
                  <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>

                              哎呀哎呀视频在线观看