Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed
declare(strict_types=1);

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;
use App\Entities\Page;

Yassine Doghri
committed
use App\Models\PageModel;
use CodeIgniter\Exceptions\PageNotFoundException;

Yassine Doghri
committed
class PageController extends BaseController

Yassine Doghri
committed
{
protected Page $page;

Yassine Doghri
committed
public function _remap(string $method, string ...$params): mixed

Yassine Doghri
committed
{

Yassine Doghri
committed
if (count($params) === 0) {
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
if (
($page = (new PageModel())->where('slug', $params[0])->first()) === null

Yassine Doghri
committed
) {
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
$this->page = $page;
return $this->{$method}();

Yassine Doghri
committed
}
public function index(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
$cacheName = "page-{$this->page->slug}";
if (! ($found = cache($cacheName))) {
$data = [
'page' => $this->page,
];

Yassine Doghri
committed
$found = view('page', $data);
// The page cache is set to a decade so it is deleted manually upon page update
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}