49 lines
919 B
PHP
49 lines
919 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// namespace MyProject\Controllers;
|
|
|
|
|
|
use MyProject\Models\ExampleModel;
|
|
|
|
class ExampleController
|
|
{
|
|
private ExampleModel $model;
|
|
|
|
public function __construct(ExampleModel $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function index(): void
|
|
{
|
|
$data = $this->model->getData();
|
|
$this->render('views/example_view.php', ['data' => $data]);
|
|
}
|
|
|
|
public function show(int $id): void
|
|
{
|
|
$item = $this->model->find($id);
|
|
|
|
if ($item === null) {
|
|
$this->notFound();
|
|
return;
|
|
}
|
|
|
|
$this->render('views/item_view.php', ['item' => $item]);
|
|
}
|
|
|
|
private function render(string $viewPath, array $data): void
|
|
{
|
|
extract($data);
|
|
include $viewPath;
|
|
}
|
|
|
|
private function notFound(): void
|
|
{
|
|
http_response_code(404);
|
|
include 'views/404.php';
|
|
}
|
|
}
|