This commit is contained in:
f4ilji
2025-01-13 13:56:50 +05:00
parent fab57a3306
commit 6b48c31afd
11 changed files with 115 additions and 62 deletions
+40 -15
View File
@@ -20,6 +20,7 @@ use App\Models\Tag;
use Carbon\Carbon;
use Doctrine\DBAL\Schema\Column;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
@@ -128,28 +129,52 @@ class ClientPostController extends Controller
public function show(Request $request, $slug)
{
$post = new PostResource(Post::where('slug', $slug)->where('publish_at', '<', Carbon::now())->firstOrFail());
$routeUrl = route('client.post.index');
$path = ltrim(parse_url($routeUrl, PHP_URL_PATH), '/');
// Уникальный ключ для кеширования
$cacheKey = 'post_' . md5($slug);
$page = Page::where('path', '=', $path)->with('section.pages.section', 'section.mainSection')->first();
// Пытаемся получить данные из кеша
$data = Cache::remember($cacheKey, now()->addHours(1), function () use ($slug) {
// Получаем пост
$post = Post::where('slug', $slug)
->where('publish_at', '<', Carbon::now())
->firstOrFail();
if (isset($page->section)) {
$breadcrumbs = [
'mainSection' => new ClientBreadcrumbSection($page->section->mainSection),
'subSection' => new ClientBreadcrumbSubSection($page->section),
'page' => new ClientBreadcrumbPage($page),
];
} else {
// Преобразуем пост в ресурс
$postResource = new PostResource($post);
// Получаем путь для страницы
$routeUrl = route('client.post.index');
$path = ltrim(parse_url($routeUrl, PHP_URL_PATH), '/');
// Получаем данные страницы
$page = Page::where('path', '=', $path)
->with('section.pages.section', 'section.mainSection')
->first();
// Формируем хлебные крошки
$breadcrumbs = null;
}
if (isset($page->section)) {
$breadcrumbs = [
'mainSection' => new ClientBreadcrumbSection($page->section->mainSection),
'subSection' => new ClientBreadcrumbSubSection($page->section),
'page' => new ClientBreadcrumbPage($page),
];
}
$seo = $post->seo ?? null;
// SEO-данные
$seo = $post->seo ?? null;
// Возвращаем данные для кеширования
return [
'post' => $postResource,
'breadcrumbs' => $breadcrumbs,
'seo' => $seo,
];
});
return Inertia::render('Client/Posts/Show', compact('post', 'breadcrumbs', 'seo'));
// Возвращаем ответ с использованием кешированных данных
return Inertia::render('Client/Posts/Show', $data);
}
}
+11 -6
View File
@@ -15,6 +15,7 @@ use App\Models\MainSection;
use App\Models\Page;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;
@@ -23,7 +24,16 @@ class PageController extends Controller
{
public function render($path)
{
$page = Page::where('path', '=', $path)->with('section.pages.section', 'section.mainSection')->first();
// Генерируем уникальный ключ для кеширования
$cacheKey = 'page_' . md5($path);
// Пытаемся получить данные из кеша
$page = Cache::remember($cacheKey, now()->addHours(1), function () use ($path) {
return Page::where('path', '=', $path)
->with('section.pages.section', 'section.mainSection')
->first();
});
if ($page === null) {
abort(404);
}
@@ -42,21 +52,16 @@ class PageController extends Controller
$seo = $page->seo ?? null;
$page = new PageResource($page);
$error = $page->code;
if ($page->code != 200) {
abort($error);
}
return Inertia::render($page->template, compact('page', 'subSectionPages', 'breadcrumbs', 'seo'));
}
public function getRegisteredPages()
{
$pages = PageResource::collection(Page::query()
@@ -13,6 +13,5 @@ class NavigateController extends Controller
public function index()
{
return ClientNavigationResource::collection(MainSection::with('subSections.pages')->orderBy('sort', 'asc')->get());
}
}