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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 響應 ## 創建響應 ### 字符串 & 數組 ``` Route::get('/', function () { return 'Hello World'; }); // 框架會自動將數組轉化為一個 JSON 響應 Route::get('/', function () { return [1, 2, 3]; }); ``` >[success] 提示:從路由或控制器返回`Eloquent 集合`也會被自動轉化為 JSON 響應。 ### Response 對象 ``` Route::get('home', function () { return response('Hello World', 200) ->header('Content-Type', 'text/plain'); }); ``` ### 添加響應頭 ``` return response($content) ->header('Content-Type', $type) ->header('X-Header-One', 'Header Value') ->header('X-Header-Two', 'Header Value'); return response($content) ->withHeaders([ 'Content-Type' => $type, 'X-Header-One' => 'Header Value', 'X-Header-Two' => 'Header Value', ]); ``` ### 緩存控制中間件 Laravel 內置了一個`cache.headers`中間件,可以用來快速地為路由組設置`Cache-Control`頭信息。如果在指令集中聲明了`etag`,Laravel 會自動將 ETag 標識符設置為響應內容的 MD5 哈希值 ``` Route::middleware('cache.headers:public;max_age=2628000;etag')->group(function() { Route::get('privacy', function () { // ... }); Route::get('terms', function () { // ... }); }); ``` ### 添加 Cookies 到響應 ``` // 模板設置cookie return response()->view('welcome')->cookie('name', 'value', $minutes); return response()->view('welcome')->withCookie('name', 'value', $minutes); // 設置 Cookies ,名稱、值、過期時間等,與`setcookie`方法的參數類似 return response($content) ->header('Content-Type', $type) ->cookie('name', 'value', $minutes [, $path, $domain, $secure, $httpOnly]); // Cookie 隊列,`queue`方法接受一個`Cookie`實例或者創建實例所需的參數 Cookie::queue(Cookie::make('name', 'value', $minutes)); Cookie::queue('name', 'value', $minutes); // 生成 Cookie 實例 $cookie = cookie('name', 'value', $minutes); return response('Hello World')->cookie($cookie); ``` ### 刪除 Cookie ``` $cookie = Cookie::forget('name' [, $path, $domain]); return response()->view('welcome')->cookie($cookie); return response()->view('welcome')->cookie('name', null, -2628000 [, $path, $domain]); ``` ### Cookies & 加密 默認情況下,Laravel 生成的所有 Cookie 都是經過加密和簽名,因此不能被客戶端修改或讀取。 如果你想要應用程序生成的部分 Cookie 不被加密,那么可以使用在`app/Http/Middleware`目錄中`App\Http\Middleware\EncryptCookies`中間件的`$except`屬性。 ``` /** * 不需要被加密的cookies名稱 * * @var array */ protected $except = [ 'cookie_name', ]; ``` ## 重定向 ``` // 全局輔助函數 Route::get('dashboard', function () { return redirect('home/dashboard'); }); // 表單提交失敗后退之前頁面 // 由于該功能利用了會話控制,需要確保調用 back 函數的路由使用 web中間件組 或所有 Session中間件 Route::post('user/profile', function () { //驗證請求 return back()->withInput(); }); ``` ### 重定向到命名路由 如果調用不帶參數的輔助函數`redirect`時,會返回`Illuminate\Routing\Redirector`實例。這個實例允許你調用`Redirector`上的任何方法。 ``` return redirect()->route('login'); // 對于具有以下 URI 的路由: profile/{id} return redirect()->route('profile', ['id' => 1]); // 通過 Eloquent 模型填充參數,自動從模型提取ID return redirect()->route('profile', [$user]); ``` 如果要自定義這個路由參數的默認參數值,需要重寫模型實例上的 getRouteKey 方法 ``` public function getRouteKey() { return $this->slug; } ``` ### 跳轉到控制器 Action ``` return redirect()->action('HomeController@index'); // 參數設置 return redirect()->action( 'UserController@profile', ['id' => 1] ); ``` ### 跳轉到外部域名 ``` // 創建不帶有任何額外的 URL 編碼、有效性校驗和檢查的`RedirectResponse`實例 return redirect()->away('https://www.google.com'); ``` ### 帶有傳送 Session 值的跳轉 ``` Route::post('user/profile', function () { // Update the user's profile... return redirect('dashboard')->with('status', 'Profile updated!'); }); // 跳轉后前臺顯示 @if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif ``` ### 帶有 Cookie 的跳轉 ``` // 使用全局輔助函數 return redirect('test')->cookie('name', 'value', 1); return redirect('test')->withCookie('name', 'value', 1); // 使用 Redirect Facades return Redirect::to('test')->cookie('name', 'value', 1); return Redirect::route('test')->withCookie('name', 'value', 1); // 使用 withCookies 設置多個 Cookie $cookie1 = cookie('name1', 'value1', 1); $cookie2 = cookie('name2', 'value2', 1); return Redirect::action('IndexController@test')->withCookies([$cookie1, $cookie2]); ``` ## 其它的響應類型 ### 視圖響應 ``` // 控制響應狀態和頭信息 return response() ->view('hello', $data, 200) ->header('Content-Type', $type); // 如果不需要傳遞自定義的 HTTP 狀態碼和自定義頭信息,可以使用全局 view 輔助函數。 return view('hello', $data); ``` ### JSON 響應 ``` return response()->json([ 'name' => 'Abigail', 'state' => 'CA' ]); ``` ### JSONP 響應 ``` return response() ->json(['name' => 'Abigail', 'state' => 'CA']) ->withCallback($request->input('callback')); ``` ### 文件下載 ``` // 注意:Symfony HttpFoundation 要求下載的文件有一個 ASCII 文件名。 return response()->download($pathToFile); // 第二個參數為用戶下載文件的文件名,第三個參數為 HTTP 頭信息數組 return response()->download($pathToFile, $name, $headers); // 下載后刪除文件 return response()->download($pathToFile)->deleteFileAfterSend(); // 流下載,字符串響應轉換為下載響應,不需要寫入磁盤 return response()->streamDownload(function () { echo GitHub::api('repo') ->contents() ->readme('laravel', 'laravel')['contents']; }, 'laravel-readme.md' [, $headers]); ``` ### 文件響應 ``` // 直接在瀏覽器顯示圖片或PDF等 return response()->file($pathToFile); return response()->file($pathToFile, $headers); ``` ## 響應宏 自定義在路由或控制器中復用的響應 ``` // `macro`方法接受一個名稱作為第一個參數,閉包函數作為的第二個參數。 <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Response; class ResponseMacroServiceProvider extends ServiceProvider { /** * 注冊應用程序的響應宏 * * @return void */ public function boot() { Response::macro('caps', function ($value) { return Response::make(strtoupper($value)); }); } } // 響應宏的閉包在`ResponseFactory`實現類或輔助函數`response`中調用宏名稱的時候被執行 return response()->caps('foo'); ```
                  <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>

                              哎呀哎呀视频在线观看