所以我正在使用我在网上找到的骨架......
“boostrap.php”看起来像
use DI\ContainerBuilder;
use Slim\App;
require_once __DIR__ . '/../vendor/autoload.php';
$containerBuilder = new ContainerBuilder();
// Add DI container definitions
$containerBuilder->addDefinitions(__DIR__ . '/container.php');
// Create DI container instance
$container = $containerBuilder->build();
// Create Slim App instance
$app = $container->get(App::class);
// Register routes
(require __DIR__ . '/routes.php')($app);
// Register middleware
(require __DIR__ . '/middleware.php')($app);
return $app;
在容器中,我添加了 TWIG,我认为是正确的......
return [
'settings' => function () {
return require __DIR__ . '/settings.php';
},
App::class => function (ContainerInterface $container) {
AppFactory::setContainer($container);
return AppFactory::create();
},
ErrorMiddleware::class => function (ContainerInterface $container) {
$app = $container->get(App::class);
$settings = $container->get('settings')['error'];
return new ErrorMiddleware(
$app->getCallableResolver(),
$app->getResponseFactory(),
(bool)$settings['display_error_details'],
(bool)$settings['log_errors'],
(bool)$settings['log_error_details']
);
},
Twig:class => function (ContainerInterface $container){
return new Twig(__DIR__.'/../views',[
'cache' => __DIR__ . '/../cache',
'auto_reload' => true
]);
},
TwigMiddleware::class => function (ContainerInterface $container){
return TwigMiddleware::createFromContainer($container->get(App::class));
},
etc etc etc
所以我认为这是正确的,但是我还需要为 Twig 设置中间件,并且还能够在 Classes/Controller/Routes 中使用它......
在我的中间件中
<?php
use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\TwigMiddleware;
use Slim\Views\Twig;
return function (App $app) {
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
$app->add(TwigMiddleware::class));
// Add the Slim built-in routing middleware
$app->addRoutingMiddleware();
$app->add(BasePathMiddleware::class);
// Handle exceptions
$app->add(ErrorMiddleware::class);
};
当前错误是
Message: The specified container key does not exist: view
File: /home/webuser/testsite/genapp/vendor/slim/twig-view/src/TwigMiddleware.php
所以我不确定如何设置我猜测的 Twig 实例/类的密钥。
我是这种 DI 格式的新手,所以我不确定如何添加中间件,然后在整个应用程序中使用它......
谢谢