teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2019/12/29 06:57

投稿

creative_09
creative_09

スコア80

title CHANGED
File without changes
body CHANGED
@@ -116,4 +116,137 @@
116
116
  のようになっています。
117
117
  なぜ読み込めていないのかわかりません
118
118
 
119
- 以上、よろしくおねがいします
119
+ 以上、よろしくおねがいします
120
+
121
+ 追記
122
+ TwigMiddleware.phpの中身です
123
+ ```ここに言語を入力
124
+ <?php
125
+ /**
126
+ * Slim Framework (http://slimframework.com)
127
+ *
128
+ * @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
129
+ */
130
+
131
+ declare(strict_types=1);
132
+
133
+ namespace Slim\Views;
134
+
135
+ use Psr\Container\ContainerInterface;
136
+ use Psr\Http\Message\ResponseInterface;
137
+ use Psr\Http\Message\ServerRequestInterface;
138
+ use Psr\Http\Server\MiddlewareInterface;
139
+ use Psr\Http\Server\RequestHandlerInterface;
140
+ use RuntimeException;
141
+ use Slim\App;
142
+ use Slim\Interfaces\RouteParserInterface;
143
+
144
+ class TwigMiddleware implements MiddlewareInterface
145
+ {
146
+ /**
147
+ * @var Twig
148
+ */
149
+ protected $twig;
150
+
151
+ /**
152
+ * @var RouteParserInterface
153
+ */
154
+ protected $routeParser;
155
+
156
+ /**
157
+ * @var string
158
+ */
159
+ protected $basePath;
160
+
161
+ /**
162
+ * @var string|null
163
+ */
164
+ protected $attributeName;
165
+
166
+ /**
167
+ * @param App $app
168
+ * @param string $containerKey
169
+ *
170
+ * @return TwigMiddleware
171
+ */
172
+ public static function createFromContainer(App $app, string $containerKey = 'view'): self
173
+ {
174
+ $container = $app->getContainer();
175
+ if ($container === null) {
176
+ throw new RuntimeException('The app does not have a container.');
177
+ }
178
+ if (!$container->has($containerKey)) {
179
+ throw new RuntimeException(
180
+ "The specified container key does not exist: $containerKey"
181
+ );
182
+ }
183
+
184
+ $twig = $container->get($containerKey);
185
+ if (!($twig instanceof Twig)) {
186
+ throw new RuntimeException(
187
+ "Twig instance could not be resolved via container key: $containerKey"
188
+ );
189
+ }
190
+
191
+ return new self(
192
+ $twig,
193
+ $app->getRouteCollector()->getRouteParser(),
194
+ $app->getBasePath()
195
+ );
196
+ }
197
+
198
+ /**
199
+ * @param App $app
200
+ * @param Twig $twig
201
+ * @param string $attributeName
202
+ *
203
+ * @return TwigMiddleware
204
+ */
205
+ public static function create(App $app, Twig $twig, string $attributeName = 'view'): self
206
+ {
207
+ return new self(
208
+ $twig,
209
+ $app->getRouteCollector()->getRouteParser(),
210
+ $app->getBasePath(),
211
+ $attributeName
212
+ );
213
+ }
214
+
215
+ /**
216
+ * @param Twig $twig
217
+ * @param RouteParserInterface $routeParser
218
+ * @param string $basePath
219
+ * @param string|null $attributeName
220
+ */
221
+ public function __construct(
222
+ Twig $twig,
223
+ RouteParserInterface $routeParser,
224
+ string $basePath = '',
225
+ ?string $attributeName = null
226
+ ) {
227
+ $this->twig = $twig;
228
+ $this->routeParser = $routeParser;
229
+ $this->basePath = $basePath;
230
+ $this->attributeName = $attributeName;
231
+ }
232
+
233
+ /**
234
+ * {@inheritdoc}
235
+ */
236
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
237
+ {
238
+ $runtimeLoader = new TwigRuntimeLoader($this->routeParser, $request->getUri(), $this->basePath);
239
+ $this->twig->addRuntimeLoader($runtimeLoader);
240
+
241
+ $extension = new TwigExtension();
242
+ $this->twig->addExtension($extension);
243
+
244
+ if ($this->attributeName !== null) {
245
+ $request = $request->withAttribute($this->attributeName, $this->twig);
246
+ }
247
+
248
+ return $handler->handle($request);
249
+ }
250
+ }
251
+
252
+ ```