하나의 옵션으로 $request->input('format') === 'json'
를 통해 AJAX 호출이있는 경우
public function main(Request $request)
{
$data = [
'categories' => /* ... */
];
if ($request->input('format') === 'json') {
return response()->json(data);
}
return view('main', $data);
}
가 또는 당신은 단순히 확인할 수 있습니다을 것 이를 위해 Middleware
을 사용하십시오. 아래 예에서는 항상 view('...', [/* some data */])
을 반환한다고 가정합니다.
다음은 형식이 json
이어야하는지 확인한 후 응답에서보기에 붙여 넣은 데이터를 가져 와서 대신 반환합니다. 그런 다음 json
및 html
을 반환 할 수있는 경로에이 미들웨어를 적용하면됩니다.
public function handle($request, Closure $next)
{
$response = $next($request);
if ($request->input('format') == 'json') {
$response->setContent(
$this->getDataFromResponse($response)
);
}
return $response;
}
/**
* Get the data that was pasted to the view
*
* @param \Illuminate\Http\Response $response
*/
protected function getDataFromResponse($response)
{
$content = $response->getOriginalContent();
return $content->getData();
}
희망이 있습니다.
어떻게 응답을 'HTML'형식으로 지정 하시겠습니까? –
'View'가 리턴되어야합니다 – Darama
지금까지 시도한 것이 있습니까? –