Added $container
This commit is contained in:
@@ -72,10 +72,15 @@ SQL
|
||||
);
|
||||
|
||||
// -------------------------
|
||||
// Services (instantiation simple et explicite)
|
||||
// Services (container simple)
|
||||
// -------------------------
|
||||
$twig = new Twig(new FilesystemLoader(__DIR__ . '/../views'), ['cache' => false]);
|
||||
$postRepository = new App\Repositories\PostRepositoryMedoo($database);
|
||||
$container = [];
|
||||
|
||||
// Vue Twig
|
||||
$container['view'] = new Twig(new FilesystemLoader(__DIR__ . '/../views'), ['cache' => false]);
|
||||
|
||||
// Repository Post (Medoo)
|
||||
$container['postRepository'] = new App\Repositories\PostRepositoryMedoo($database);
|
||||
|
||||
// -------------------------
|
||||
// Slim app
|
||||
@@ -96,9 +101,9 @@ if (!$isDebug) {
|
||||
|
||||
// Middlewares essentiels
|
||||
$app->addBodyParsingMiddleware();
|
||||
$app->add(TwigMiddleware::create($app, $twig));
|
||||
$app->add(TwigMiddleware::create($app, $container['view']));
|
||||
|
||||
// Charger routes (web.php reçoit explicitement $twig et $postRepository)
|
||||
(require __DIR__ . '/../src/Routes/web.php')($app, $twig, $postRepository);
|
||||
// Charger routes (web.php reçoit maintenant le container)
|
||||
(require __DIR__ . '/../src/Routes/web.php')($app, $container);
|
||||
|
||||
$app->run();
|
||||
|
||||
@@ -7,8 +7,14 @@ use Slim\Views\Twig;
|
||||
use App\Repositories\PostRepositoryMedoo;
|
||||
use App\Controllers\PostController;
|
||||
|
||||
return function (App $app, Twig $twig, PostRepositoryMedoo $repo): void {
|
||||
$controller = new PostController($twig, $repo);
|
||||
return function (App $app, array $container): void {
|
||||
/** @var Twig $view */
|
||||
$view = $container['view'];
|
||||
/** @var PostRepositoryMedoo $repo */
|
||||
$repo = $container['postRepository'];
|
||||
|
||||
// Instancier le controller une seule fois tout en gardant la modularité
|
||||
$controller = new PostController($view, $repo);
|
||||
|
||||
$app->get('/', [$controller, 'index']);
|
||||
$app->get('/admin', [$controller, 'admin']);
|
||||
|
||||
Reference in New Issue
Block a user