Мне бы хотелось, чтобы, как только запрос ajax отправлялся туда, где есть представление ('view'), вместо того, чтобы возвращать необработанное представление, он возвращает JSON
Вот так:
/**
* @param \Illuminate\Contracts\View\View $view
* @return \Illuminate\Http\JsonResponse
*/
protected function ajaxResponse (View $view)
{
return response()->json([
'id' => $view->getData()['page'],
'title' => $view->getData()['title'],
'content' => $view->renderSections()['content'],
'replacepage' => null,
]);
}
Но я абсолютно не знаю, как это сделать. Я не хочу повторяться в каждом контроллере
Я пытался заменить View на мой, но он не работает так, как я вроде все работает, но я не могу восстановить раздел моей страницы
<?php
namespace App\Extensions\View;
use Illuminate\View\View;
class AjaxView extends View
{
/**
* Get the contents of the view instance.
*
* @return string
* @throws \Throwable
*/
protected function renderContents()
{
if(request()->ajax()) {
echo $this->getResponse($this->getData());
exit;
}
return parent::renderContents();
}
/**
* @param array $data
* @return false|string
*/
public function getResponse (array $data = [])
{
return json_encode([
'id' => $data['page'],
'title' => (!empty($data['title']) ? $data['title'] . ' - ' . config('app.name') : null),
'content' => $this->renderSections()['content'],
'replacepage' => null
]);
}
}
Это возвращает:
![enter image description here](https://i.stack.imgur.com/beAfv.png)