Files
laravel-manager-packagist/src/Modules/Pages/PublicPageController.php
T
2026-12-04 16:24:51 +00:00

41 lines
1.6 KiB
PHP

<?php
namespace IriksIT\Manager\Modules\Pages;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use IriksIT\Manager\Models\Page;
use IriksIT\Manager\Support\Blocks;
/**
* The pages drawn by the package itself (`manager.frontend.mode` = package), for a site that has no frontend code of its own. It is the
* fallback route: it answers only an address nothing else answered, and only for a page that has blocks and is there (a page with a
* definition, or a free page that is published). Anything else is the usual 404.
*/
class PublicPageController
{
public function __invoke(Request $request): Response
{
$path = trim($request->path(), '/');
$slug = $path === '' ? (string) config('manager.blocks.preview.home', 'home') : $path;
abort_unless(preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $slug) === 1, 404);
$definition = Page::definitions()[$slug] ?? null;
$page = Page::query()->where('slug', $slug)->first();
abort_if($definition === null && ($page === null || ! $page->is_published), 404);
$blocks = Blocks::for($slug);
abort_if($blocks === [], 404);
return response()->view((string) config('manager.frontend.layout', 'manager::public.layout'), [
'title' => $page->title ?? $definition['title'] ?? $slug,
'description' => $page->meta_description ?? null,
'blocks' => $blocks,
'slug' => $slug,
'css' => (string) file_get_contents(__DIR__.'/../../../resources/blocks/blocks.css'),
])->header('Cache-Control', 'private, no-cache');
}
}