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