/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
src/Illuminate/Http/Middleware/PrefersJsonResponses.php
65 строк
2 KB
Wendell Adriel
[13.x] Add prefersJsonResponses() to the application builder (#59753)
20 апр 2026, 17:47
Не верифицирован
20 апр 2026, 17:47
e2ffb2c
Код
Авторство
О чём код?
<?php namespace Illuminate\Http\Middleware; use Closure; class PrefersJsonResponses { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $accept = $request->headers->get('Accept'); if ($this->acceptHeaderIsBroad($accept)) { if ($accept !== null) { $request->headers->set('X-Original-Accept', $accept); } $request->headers->set('Accept', 'application/json'); } return $next($request); } /** * Determine if the given "Accept" header value is broad enough to be treated as JSON. * * The header is broad when it's missing or every media-type listed is wildcard ("*\/*" or "application/*"). * * @param string|null $accept * @return bool */ protected function acceptHeaderIsBroad($accept) { if ($accept === null || trim($accept) === '') { return true; } foreach (explode(',', $accept) as $value) { $value = strtolower(trim($value)); if ($value === '') { continue; } $pos = strpos($value, ';'); if ($pos !== false) { $value = trim(substr($value, 0, $pos)); } if (! in_array($value, ['*/*', 'application/*'], true)) { return false; } } return true; } }