Your commit message
This commit is contained in:
parent
d37c197e5c
commit
1ff80f75e0
|
@ -2,47 +2,53 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
// namespace MyProject\Controllers;
|
namespace MyProject\Controllers;
|
||||||
|
|
||||||
|
|
||||||
use MyProject\Models\ExampleModel;
|
use MyProject\Models\ExampleModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ExampleController
|
||||||
|
*
|
||||||
|
* Handles the requests related to examples.
|
||||||
|
*/
|
||||||
class ExampleController
|
class ExampleController
|
||||||
{
|
{
|
||||||
private ExampleModel $model;
|
private ExampleModel $model;
|
||||||
|
|
||||||
public function __construct(ExampleModel $model)
|
/**
|
||||||
{
|
* Display a specific item view.
|
||||||
$this->model = $model;
|
*
|
||||||
}
|
* Retrieves an item by its ID and renders the item view. If the item is not found,
|
||||||
|
* a not found response is generated.
|
||||||
public function index(): void
|
*
|
||||||
{
|
* @param integer $id The ID of the item to retrieve.
|
||||||
$data = $this->model->getData();
|
*
|
||||||
$this->render('views/example_view.php', ['data' => $data]);
|
* @return void
|
||||||
}
|
*/
|
||||||
|
|
||||||
public function show(int $id): void
|
public function show(int $id): void
|
||||||
{
|
{
|
||||||
$item = $this->model->find($id);
|
$item = $this->model->find($id);
|
||||||
|
|
||||||
if ($item === null) {
|
if ($item === null) {
|
||||||
$this->notFound();
|
$this->notFound();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->render('views/item_view.php', ['item' => $item]);
|
$this->render('views/item_view.php', ['item' => $item]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function render(string $viewPath, array $data): void
|
/**
|
||||||
|
* Display the index view with data.
|
||||||
|
*
|
||||||
|
* Retrieves data from the model and renders the index view.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function index(): void
|
||||||
{
|
{
|
||||||
extract($data);
|
$data = $this->model->getData();
|
||||||
include $viewPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function notFound(): void
|
$this->render('views/example_view.php', ['data' => $data]);
|
||||||
{
|
|
||||||
http_response_code(404);
|
|
||||||
include 'views/404.php';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue