60 lines
3.1 KiB
PHP
60 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\CmsPage;
|
|
use App\Services\IndexNowService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
final class CmsPageController extends Controller
|
|
{
|
|
public function index() { return view('admin.cms.index', ['pages' => CmsPage::latest()->paginate(25)]); }
|
|
public function create() { return view('admin.cms.form', ['page' => new CmsPage()]); }
|
|
public function store(Request $request, IndexNowService $indexNow) { $page = CmsPage::create($this->validated($request)); $this->submitIfIndexable($page, $indexNow); return redirect()->route('admin.cms.edit', $page)->with('status', 'Page created.'); }
|
|
public function edit(CmsPage $page) { return view('admin.cms.form', compact('page')); }
|
|
public function update(Request $request, CmsPage $page, IndexNowService $indexNow) { $page->update($this->validated($request, $page)); $this->submitIfIndexable($page, $indexNow); return back()->with('status', 'Page updated.'); }
|
|
public function destroy(CmsPage $page) { $page->delete(); return redirect()->route('admin.cms.index')->with('status', 'Page deleted.'); }
|
|
|
|
private function submitIfIndexable(CmsPage $page, IndexNowService $indexNow): void
|
|
{
|
|
if (! $page->is_published || ! $page->robots_index || ($page->published_at && $page->published_at->isFuture())) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$indexNow->submit([route('cms.show', ['slug' => $page->slug])]);
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
private function validated(Request $request, ?CmsPage $page = null): array
|
|
{
|
|
$request->merge(['slug' => Str::slug($request->input('slug') ?: $request->input('title', ''))]);
|
|
$data = $request->validate([
|
|
'title' => ['required','string','max:160'],
|
|
'slug' => ['required','string','max:180', Rule::unique('cms_pages','slug')->ignore($page?->id)],
|
|
'excerpt' => ['nullable','string','max:1000'],
|
|
'body' => ['required','string'],
|
|
'meta_title' => ['nullable','string','max:160'],
|
|
'meta_description' => ['nullable','string','max:320'],
|
|
'meta_keywords' => ['nullable','string','max:500'],
|
|
'canonical_url' => ['nullable','url:http,https','max:2048'],
|
|
'og_title' => ['nullable','string','max:160'],
|
|
'og_description' => ['nullable','string','max:320'],
|
|
'og_image' => ['nullable','url:http,https','max:2048'],
|
|
'sitemap_priority' => ['required','numeric','min:0','max:1'],
|
|
'sitemap_change_frequency' => ['required', Rule::in(['always','hourly','daily','weekly','monthly','yearly','never'])],
|
|
]);
|
|
$data['robots_index'] = $request->boolean('robots_index');
|
|
$data['robots_follow'] = $request->boolean('robots_follow');
|
|
$data['include_in_sitemap'] = $request->boolean('include_in_sitemap');
|
|
$data['is_published'] = $request->boolean('is_published');
|
|
$data['published_at'] = $data['is_published'] ? ($page?->published_at ?? now()) : null;
|
|
return $data;
|
|
}
|
|
}
|