laravel5.4 | 响应
# laravel5.4 | 响应 Route::get('/', function () { return 'Hello World'; }); ## Response 对象 返回一个完整的 Response 实例允许你自定义响应的 HTTP 状态码和头信息 Route::get('home', function () { return response('Hello World', 200) ->header('Content-Type', 'text/plain'); }); #### 添加响应头 header return response($content) ->header('Content-Type', $type) ->header('X-Header-One', 'Header Value') ->header('X-Header-Two', 'Header Value') 或response return response($content) ->withHeaders([ 'Content-Type' => $type, 'X-Header-One' => 'Header Value', 'X-Header-Two' => 'Header Value', ]); #### 添加 Cookie 到响应 使用响应实例上的 cookie 方法可以轻松添加 Cookie 到响应。例如,你可以使用 cookie 方法生成 Cookie 并添加将其添加到响应实例: return response($content) ->header('Content-Type', $type) ->cookie('name', 'value', $minutes); cookie 方法还可以接收更多使用频率较低的额外可选参数,一般来说,这些参数和 PHP 原生提供的setcookie 方法目的和意义差不多: ->cookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly) #### Cookie & 加密 默认情况下,Laravel 框架生成的 Cookie 都经过了加密和签名,以免在客户端被篡改。如果你想要让特定的 Cookie 子集在生成时取消加密,可以使用 app/Http/Middleware 目录下的中间件 App\Http\Middleware\EncryptCookies 提供的 $except 属性来排除这些 Cookie: /** * 不需要被加密的cookies名称 * * @var array */ protected $except = [ 'cookie_name', ]; ## 重定向 Illuminate\Http\RedirectResponse 类的实例 或全局辅助函数 redirect: 将用户重定向到上一个请求的位置 Route::post('user/profile', function () { // 验证请求... return back()->withInput(); }); #### 重定向到命名路由 return redirect()->route('login'); 带参数 return redirect()->route('profile',['id'=>1]); Eloquent 模型绑定 可以传递模型本身,ID 会被自动解析出来 return redirect()->route('profile', [$user]); #### 自定义这个路由参数中的默认值(默认是id),需要重写模型实例上的 getRouteKey 方法: /** * Get the value of the model's route key. * * @return mixed */ public function getRouteKey() { return $this->slug; } #### 重定向到控制器动作 return redirect()->action('HomeController@index'); return redirect()->action('UserController@profile', ['id'=>1]); #### 带一次性 Session 数据的重定向 重定向到一个新的 URL 并将数据存储到一次性 Session 中通常是同时完成的,为了方便,可以创建一个 RedirectResponse 实例然后在同一个方法链上将数据存储到 Session,这种方式在 action 之后存储状态信息时特别方便: Route::post('user/profile', function () { // 更新用户属性... return redirect('dashboard')->with('status', 'Profile updated!'); }); 当然,用户重定向到新页面之后,你可以从 Session 中取出并显示这些一次性信息,例如,使用 Blade 语法实现如下: @if (session('status'))
{{ session('status') }}
@endif ## 其它响应类型 辅助函数 response #### 视图响应 如果你需要控制响应状态和响应头,并且还需要返回一个视图作为响应内容,可以使用 view 方法: return response() ->view('hello', $data, 200) ->header('Content-Type', $type); 如果你不需要传递自定义的 HTTP 状态码和头信息,只需要简单使用全局辅助函数 view 即可。 #### json响应 json 方法会自动将 Content-Type 头设置为 application/json,并使用 PHP 函数 json_encode 方法将给定数组转化为 JSON: return response()->json([ 'name' => 'Abigail', 'state' => 'CA' ]); 如果你想要创建一个 JSONP 响应,可以在 json 方法之后调用 setCallback 方法: return response() ->json(['name' => 'Abigail', 'state' => 'CA']) ->withCallback($request->input('callback')); #### 文件下载 download 方法用于生成强制用户浏览器下载给定路径文件的响应。download 方法接受文件名作为第二个参数,该参数决定用户下载文件的显示名称,你还可以将 HTTP 头信息作为第三个参数传递到该方法: return response()->download($pathToFile); return response()->download($pathToFile, $name, $headers); 注:管理文件下载的 Symfony HttpFoundation 类要求被下载文件有一个 ASCII 文件名。 #### 文件响应 file 方法可用于直接在用户浏览器显示文件,例如图片或PDF,而不需要下载,该方法接收文件路径作为第一个参数,头信息数组作为第二个参数: return response()->file($pathToFile); return response()->file($pathToFile, $headers); ## 响应宏 如果你想要定义一个自定义的可以在多个路由和控制器中复用的响应,可以使用 Response 门面上的 macro 方法。例如,在某个服务提供者的 boot 方法中编写代码如下: caps('foo'); 本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。