Skip to content

Commit 0b398ff

Browse files
guanguansjiannei
authored andcommitted
Add SetAcceptHeader middleware
1 parent 16b0021 commit 0b398ff

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the jiannei/laravel-response.
5+
*
6+
* (c) Jiannei <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Jiannei\Response\Laravel\Http\Middleware;
13+
14+
use Closure;
15+
use Illuminate\Http\Request;
16+
use Illuminate\Support\Str;
17+
18+
class SetAcceptHeader
19+
{
20+
/**
21+
* Handle an incoming request.
22+
*
23+
* @param \Illuminate\Http\Request $request
24+
* @param \Closure $next
25+
* @param string $type
26+
*
27+
* @return \Illuminate\Http\Response
28+
*/
29+
public function handle(Request $request, Closure $next, string $type = 'json')
30+
{
31+
Str::contains($request->header('Accept'), $contentType = "application/$type") or
32+
$request->headers->set('Accept', $contentType);
33+
34+
return $next($request);
35+
}
36+
}

0 commit comments

Comments
 (0)