当请求是json all ok时,我得到了我的'xxx‘头,但当它是对页面的直接请求时-我的头不存在于响应中。
我尝试了response()->header
,response('content',200,['xxx'=>'xxx'])
的任何变体,都没有成功
我在控制器中的代码:
return $request->wantsJson()
? response([
'html' => view('components.tarif-list', compact('tarifs'))->render(),
'pagination' => $tarifs->appends(request()->query())->links()->toHtml(),
'head' => $seo_block,
'stats' => $stats
])->header('xxx','xxx')
: response(view('home',compact('tarifs','seo_block'))->render())->header('xxx','xxx');
home.blade.php
@extends('layouts.main')
@section('content')
<x-filters/>
<div id="head" class="alert alert-success" role="alert">
@if(!empty($seo_block))
{!! $seo_block !!}
@elseif($tarifs->isEmpty())
empty
@else
hello
@endif
</div>
{{$tarifs->appends(request()->query())->links()}}
<table id="tarif-list" class="table table-hover table-striped">
<x-tarif-list :tarifs="$tarifs"/>
</table>
{{$tarifs->appends(request()->query())->links()}}
@endsection
发布于 2020-05-07 19:32:07
如果你看了这行代码
response(view('home',compact('tarifs','seo_block'))->render())->header('xxx','xxx')
您将看到以下响应
Symfony\Component\HttpFoundation\ResponseHeaderBag {#2878
#computedCacheControl: array:2 [
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:3 [
"cache-control" => "Cache-Control"
"date" => "Date"
"xxx" => "xxx"
]
#headers: array:3 [
"cache-control" => array:1 [
0 => "no-cache, private"
]
"date" => array:1 [
0 => "Thu, 07 May 2020 11:27:31 GMT"
]
"xxx" => array:1 [
0 => "xxx"
]
]
#cacheControl: []
}
密钥已经存在了,但是如何在刀片文件中检索头文件呢?
发布于 2020-05-07 21:09:30
在响应为dump()
之前,问题出在控制器中,而且我们知道标题必须是页面的第一个内容。
但为什么php或laravel没有说明这一点呢?默认php警告:
PHP Warning: Cannot modify header information - headers already sent by..........
https://stackoverflow.com/questions/61656581
复制相似问题