Changes
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -6,9 +6,10 @@ use App\Http\Middleware\AccessCheck;
|
||||
use App\Http\Middleware\RateLimitCheckMiddleware;
|
||||
use App\Http\Middleware\RateLimitCounterMiddleware;
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
use Spatie\Permission\Middlewares\PermissionMiddleware;
|
||||
use Spatie\Permission\Middlewares\RoleMiddleware;
|
||||
use Spatie\Permission\Middlewares\RoleOrPermissionMiddleware;
|
||||
use Spatie\Permission\Middleware\PermissionMiddleware;
|
||||
use Spatie\Permission\Middleware\RoleMiddleware;
|
||||
use Spatie\Permission\Middleware\RoleOrPermissionMiddleware;
|
||||
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Middleware;
|
||||
use App\Http\Resources\ClientNavigationResource;
|
||||
use App\Models\MainSection;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Middleware;
|
||||
use Tightenco\Ziggy\Ziggy;
|
||||
|
||||
@@ -32,6 +33,14 @@ class HandleInertiaRequests extends Middleware
|
||||
*/
|
||||
public function share(Request $request): array
|
||||
{
|
||||
$navigation = Cache::remember('navigation', now()->addHours(1), function () {
|
||||
return ClientNavigationResource::collection(
|
||||
MainSection::with('subSections.pages.section')
|
||||
->orderBy('sort', 'asc')
|
||||
->get()
|
||||
);
|
||||
});
|
||||
|
||||
return [
|
||||
...parent::share($request),
|
||||
'auth' => [
|
||||
@@ -41,15 +50,14 @@ class HandleInertiaRequests extends Middleware
|
||||
...(new Ziggy)->toArray(),
|
||||
'location' => $request->url(),
|
||||
],
|
||||
'navigation' => ClientNavigationResource::collection(MainSection::with('subSections.pages.section')->orderBy('sort', 'asc')->get()),
|
||||
'navigation' => $navigation,
|
||||
'urlPrev' => function() {
|
||||
if (url()->previous() !== '' && url()->previous() !== url()->current()) {
|
||||
return url()->previous();
|
||||
} else {
|
||||
return 'empty'; // Используется в JavaScript для отключения поведения кнопки "Назад"
|
||||
return 'empty';
|
||||
}
|
||||
},
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Observers;
|
||||
|
||||
use App\Models\Post;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
|
||||
class PostObserver
|
||||
@@ -13,27 +14,24 @@ class PostObserver
|
||||
*/
|
||||
public function saved(Post $post): void
|
||||
{
|
||||
$recipient = auth()->user();
|
||||
|
||||
Notification::make()
|
||||
->title('Saved successfully')
|
||||
->sendToDatabase($recipient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Post "updated" event.
|
||||
*/
|
||||
public function updated(Post $post): void
|
||||
public function updated(Post $post)
|
||||
{
|
||||
//
|
||||
$this->clearPostCache($post);
|
||||
$this->cachePost($post); // Кешируем обновленный пост
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Post "deleted" event.
|
||||
* Очистка кеша при удалении поста.
|
||||
*/
|
||||
public function deleted(Post $post): void
|
||||
public function deleted(Post $post)
|
||||
{
|
||||
//
|
||||
$this->clearPostCache($post);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,4 +49,24 @@ class PostObserver
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
protected function cachePost(Post $post)
|
||||
{
|
||||
$cacheKey = 'post_' . md5($post->slug);
|
||||
$cacheData = [
|
||||
'post' => $post,
|
||||
'seo' => $post->seo,
|
||||
];
|
||||
|
||||
Cache::put($cacheKey, $cacheData, now()->addHours(1)); // Кешируем на 1 час
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистка кеша поста.
|
||||
*/
|
||||
protected function clearPostCache(Post $post)
|
||||
{
|
||||
$cacheKey = 'post_' . md5($post->slug);
|
||||
Cache::forget($cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Models\Post;
|
||||
use App\Observers\PageObserver;
|
||||
use App\Observers\PostObserver;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Facades\Filament;
|
||||
@@ -29,6 +31,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
setlocale(LC_TIME, 'ru_RU.UTF-8');
|
||||
Page::observe(PageObserver::class);
|
||||
Post::observe(PostObserver::class);
|
||||
Carbon::setLocale(config('app.locale'));
|
||||
Model::preventLazyLoading(!app()->isProduction());
|
||||
// URL::forceScheme('https');
|
||||
|
||||
Reference in New Issue
Block a user