This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blog-slim.old/public/index.php
2026-03-09 15:14:57 +01:00

101 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Dotenv\Dotenv;
// Charger les variables d'environnement
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
use Slim\Factory\AppFactory;
use Slim\Views\TwigMiddleware;
use Slim\Views\Twig;
use Medoo\Medoo;
use App\Controllers\PostController;
// ============================================
// Configuration
// ============================================
$env = $_ENV['APP_ENV'] ?? 'production';
$isDev = strtolower($env) === 'development';
// Dossier de cache Twig (false en dev, chemin en prod)
$twigCache = false;
if (!$isDev) {
$twigCache = __DIR__ . '/../var/cache/twig';
if (!is_dir($twigCache)) {
@mkdir($twigCache, 0755, true);
}
}
// Chemin base de données
$dbFile = __DIR__ . '/../database/app.sqlite';
$dbDir = dirname($dbFile);
if (!is_dir($dbDir)) {
@mkdir($dbDir, 0755, true);
}
if (!file_exists($dbFile)) {
@touch($dbFile);
@chmod($dbFile, 0664);
}
// ============================================
// Initialisation des services
// ============================================
// Twig
$twig = Twig::create(
__DIR__ . '/../views',
['cache' => $twigCache]
);
// Medoo (SQLite)
$db = new Medoo([
'type' => 'sqlite',
'database' => $dbFile,
]);
// Créer la table si elle n'existe pas
$db->pdo->exec("
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
");
// ============================================
// Slim App
// ============================================
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$app->add(TwigMiddleware::create($app, $twig));
// ============================================
// Routes
// ============================================
$controller = new PostController($twig, $db);
$app->get('/', [$controller, 'index']);
$app->get('/article/{slug}', [$controller, 'show']);
$app->get('/admin', [$controller, 'admin']);
$app->get('/admin/edit/{id}', [$controller, 'form']);
$app->post('/admin/create', [$controller, 'create']);
$app->post('/admin/edit/{id}', [$controller, 'update']);
$app->post('/admin/delete/{id}', [$controller, 'delete']);
// ============================================
// Run
// ============================================
$errorMiddleware = $app->addErrorMiddleware($isDev, $isDev, $isDev);
$app->run();