はじめてslimを使用しようとおもい、
slimをcomposerで作業ディレクトリにインストールし、
ビルトインサーバーで実行しています。
正常にパラメーターなどは渡せているのですが
twigを入れるとエラーがでます。
どうしたらよいのか教えて下さい
php7.3
slim4
composer require slim/slim
composer require slim/psr7
素の状態から行った変更は
public/index.php
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; use \Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/', function (Request $request, Response $response) { $response->getBody()->write("Hello Slim!!"); return $response; }); $app->run();
php -S localhost:8080 -t public public/index.php
この時点でパラメーターなども渡せており正常に表示されています
次に
composer require slim/twig-view
composer require php-di/php-di
templates/index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Index</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css"> </head> <body class="container"> <h1 class="display-4">Index</h1> <p>Hello, Slim + Twig !!</p> </body> </html>
publicフォルダ内index.phpの中身を以下に変更
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; use \DI\Container; use \Slim\Views\Twig; use \Slim\Views\TwigExtension; use \Slim\Views\TwigMiddleware; use \Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; // DI Container $container = new Container(); // create App AppFactory::setContainer($container); $app = AppFactory::create(); // add TwigMiddleware $routeParser = $app->getRouteCollector()->getRouteParser(); $twig = new Twig('./templates', ['cache' => './templates/cache']); $twigMiddleware = new TwigMiddleware($twig, $container, $routeParser, '/'); $app->add($twigMiddleware); // '/' route $app->get('/', function ($request, $response) { return $this->get('view')->render($response, 'index.html'); }); $app->run();
php -S localhost:8080 -t public public/index.php
すると
( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Slim\Views\Twig::__construct() must be an instance of Twig\Loader\LoaderInterface, string given, called in D:\作業フォルダ\slim\public\index.php on line 25 and defined in D:\作業フォルダ\slim\vendor\slim\twig-view\src\Twig.php on line 104
( ! ) TypeError: Argument 1 passed to Slim\Views\Twig::__construct() must be an instance of Twig\Loader\LoaderInterface, string given, called in D:\作業フォルダ\slim\public\index.php on line 25 in D:\作業フォルダ\slim\vendor\slim\twig-view\src\Twig.php on line 104
上記のエラーが出ます
twig.phpの104行目ですが
public function __construct(LoaderInterface $loader, array $settings = [])
で
/** * @param LoaderInterface $loader Twig loader * @param array $settings Twig environment settings */ public function __construct(LoaderInterface $loader, array $settings = []) { $this->loader = $loader; $this->environment = new Environment($this->loader, $settings); }
のようになっています。
なぜ読み込めていないのかわかりません
以上、よろしくおねがいします
追記
TwigMiddleware.phpの中身です
<?php /** * Slim Framework (http://slimframework.com) * * @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License) */ declare(strict_types=1); namespace Slim\Views; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use RuntimeException; use Slim\App; use Slim\Interfaces\RouteParserInterface; class TwigMiddleware implements MiddlewareInterface { /** * @var Twig */ protected $twig; /** * @var RouteParserInterface */ protected $routeParser; /** * @var string */ protected $basePath; /** * @var string|null */ protected $attributeName; /** * @param App $app * @param string $containerKey * * @return TwigMiddleware */ public static function createFromContainer(App $app, string $containerKey = 'view'): self { $container = $app->getContainer(); if ($container === null) { throw new RuntimeException('The app does not have a container.'); } if (!$container->has($containerKey)) { throw new RuntimeException( "The specified container key does not exist: $containerKey" ); } $twig = $container->get($containerKey); if (!($twig instanceof Twig)) { throw new RuntimeException( "Twig instance could not be resolved via container key: $containerKey" ); } return new self( $twig, $app->getRouteCollector()->getRouteParser(), $app->getBasePath() ); } /** * @param App $app * @param Twig $twig * @param string $attributeName * * @return TwigMiddleware */ public static function create(App $app, Twig $twig, string $attributeName = 'view'): self { return new self( $twig, $app->getRouteCollector()->getRouteParser(), $app->getBasePath(), $attributeName ); } /** * @param Twig $twig * @param RouteParserInterface $routeParser * @param string $basePath * @param string|null $attributeName */ public function __construct( Twig $twig, RouteParserInterface $routeParser, string $basePath = '', ?string $attributeName = null ) { $this->twig = $twig; $this->routeParser = $routeParser; $this->basePath = $basePath; $this->attributeName = $attributeName; } /** * {@inheritdoc} */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $runtimeLoader = new TwigRuntimeLoader($this->routeParser, $request->getUri(), $this->basePath); $this->twig->addRuntimeLoader($runtimeLoader); $extension = new TwigExtension(); $this->twig->addExtension($extension); if ($this->attributeName !== null) { $request = $request->withAttribute($this->attributeName, $this->twig); } return $handler->handle($request); } }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/08 15:34