以下是路线的一些Laravel代码:
Route::get('a', function() {
return 'hello';
});
调用此路线时,Content-Type
标头值为text/HTML
。
此默认行为来自哪里?它设置为:
答案 0 :(得分:1)
Laravel扩展了Symphony的请求/响应,但仍然严重依赖于他们的默认行为。此特定功能基于Symphony的Response::prepare功能。
public function prepare(Request $request)
{
//does a lot of things, would not go into detail
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getRequestFormat();
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
}
//Does a lot more things
}
这是基于$request->getMimeType($format)
函数,而$format
来自$request->getRequestFormat()
public function getRequestFormat($default = 'html')
{
if (null === $this->format) {
$this->format = $this->attributes->get('_format');
}
return null === $this->format ? $default : $this->format;
}
注意默认值是" html"当没有明确设置请求格式时。它也只会在您尚未明确设置的情况下进行设置。
mime类型基于在https://github.com/symfony/http-foundation/blob/master/Request.php#L1844
初始化的查找框架的确超出了这个水平,并提供了替代方案,例如: JsonResponse
(laravel在执行response()->json()
时使用的内容)并且此响应使用不同的默认值。
答案 1 :(得分:0)
laravel会自动将字符串转换为完整的HTTP响应。
我在symfony / http-foundation / response.php中找到了这个方法。可能是这个方法用于创建响应头。
public function prepare(Request $request)
{
$headers = $this->headers;
if ($this->isInformational() || $this->isEmpty()) {
$this->setContent(null);
$headers->remove('Content-Type');
$headers->remove('Content-Length');
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getRequestFormat();
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
}
// Fix Content-Type
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
}
// Fix Content-Length
if ($headers->has('Transfer-Encoding')) {
$headers->remove('Content-Length');
}
if ($request->isMethod('HEAD')) {
// cf. RFC2616 14.13
$length = $headers->get('Content-Length');
$this->setContent(null);
if ($length) {
$headers->set('Content-Length', $length);
}
}
}
// Fix protocol
if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
$this->setProtocolVersion('1.1');
}
// Check if we need to send extra expire info headers
if ('1.0' == $this->getProtocolVersion() && false !== strpos($this->headers->get('Cache-Control'), 'no-cache')) {
$this->headers->set('pragma', 'no-cache');
$this->headers->set('expires', -1);
}
$this->ensureIEOverSSLCompatibility($request);
return $this;
}
答案 2 :(得分:0)
使用路径时,标题值由laravel设置。这是symfony/http-foundation/Response.php
public function __construct($content = '', $status = 200, $headers = array())
{
$this->headers = new ResponseHeaderBag($headers);
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');
/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}
}
希望这有帮助!