Initial boilerplate commit part 1.

This commit is contained in:
2026-03-31 08:23:46 +02:00
parent 9562f8f826
commit 44fb025c81
7 changed files with 906 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
abstract class BaseController
{
private Psr17Factory $factory;
public function __construct()
{
$this->factory = new Psr17Factory();
}
/**
* Render a template — the template itself is responsible for including any layout.
*/
protected function render(string $template, array $data = [], int $status = 200): ResponseInterface
{
ob_start();
extract($data);
require __DIR__ . '/../views/' . $template . '.php';
$html = ob_get_clean();
return $this->response($html, $status);
}
/**
* Automatically return a partial for HTMX requests, full page otherwise.
* Each template is responsible for including its own layout if needed.
*/
protected function view(
ServerRequestInterface $request,
string $fullTemplate,
string $partialTemplate,
array $data = []
): ResponseInterface {
$template = $this->isHtmx($request) ? $partialTemplate : $fullTemplate;
return $this->render($template, $data);
}
/**
* Return a JSON response.
*/
protected function json(array $data, int $status = 200): ResponseInterface
{
return $this->response(json_encode($data), $status, 'application/json');
}
/**
* Check if the request came from HTMX.
*/
protected function isHtmx(ServerRequestInterface $request): bool
{
return $request->hasHeader('HX-Request');
}
/**
* Build a basic HTML response.
*/
private function response(string $body, int $status = 200, string $contentType = 'text/html'): ResponseInterface
{
$response = $this->factory->createResponse($status);
$response->getBody()->write($body);
return $response->withHeader('Content-Type', $contentType);
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class HomeController extends BaseController
{
public function index(ServerRequestInterface $request): ResponseInterface
{
return $this->render('pages/home');
}
}
+2
View File
@@ -0,0 +1,2 @@
<p> Jason testing, Here! </p>