回答編集履歴
4
具体的な例を記述しています。
test
CHANGED
@@ -2,13 +2,105 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
以下が、
|
5
|
+
以下が、パソコン環境です。
|
6
|
+
|
7
|
+
|
8
|
+
|
6
|
-
|
9
|
+
PHP 7.3.12
|
10
|
+
|
11
|
+
Slim/Slim 4.4
|
12
|
+
|
13
|
+
Slim/psr7 1.0
|
14
|
+
|
15
|
+
PHP-di/PHP-di 6.0
|
16
|
+
|
17
|
+
Slim/twig-view 3.0
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
さらに、関係するドキュメントを記述しています。
|
22
|
+
|
23
|
+
|
24
|
+
|
7
|
-
___________________________________________________________
|
25
|
+
___________________________________________________________
|
26
|
+
|
8
|
-
|
27
|
+
(1)本に書かれたままで、「http://localhost:8080/」を実行した結果
|
28
|
+
|
29
|
+
|
30
|
+
|
9
|
-
|
31
|
+
```
|
32
|
+
|
10
|
-
|
33
|
+
Fatal error: Uncaught TypeError:
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
Argument 1 passed to Slim\Views\Twig::__construct() must be an instance of Twig\Loader\LoaderInterface, string given, called in C:\Users\xxxx\Desktop\my_slim_app\app\middleware.php on line 19
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
called in C:\Users\xxxx\Desktop\my_slim_app\app\middleware.php on line 19
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
and defined in C:\Users\xxxx\Desktop\my_slim_app\vendor\slim\twig-view\src\Twig.php:104
|
46
|
+
|
47
|
+
Stack trace: #0
|
48
|
+
|
49
|
+
C:\Users\xxxx\Desktop\my_slim_app\app\middleware.php(19): Slim\Views\Twig->__construct('../templates', Array) #1
|
50
|
+
|
51
|
+
C:\Users\xxxx\Desktop\my_slim_app\public\index.php(44): {closure}(Object(Slim\App)) #2
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
{main} thrown in C:\Users\xxxx\Desktop\my_slim_app\vendor\slim\twig-view\src\Twig.php on line 104
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
___________________________________________________________
|
62
|
+
|
63
|
+
(2)そのため、(4)と(5)の修正をして、「http://localhost:8080/」を実行した結果
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
Hello 2 world!
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
___________________________________________________________
|
76
|
+
|
77
|
+
(3)さらに、(4)と(5)の修正のままで、「http://localhost:8080/HANAKO」を実行した結果
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
"statusCode": 500,
|
86
|
+
|
87
|
+
"error": {
|
88
|
+
|
89
|
+
"type": "SERVER_ERROR",
|
90
|
+
|
91
|
+
"description": "No entry or class found for 'view'"
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
___________________________________________________________
|
102
|
+
|
11
|
-
my_slim_app/app/middleware.php
|
103
|
+
(4)「my_slim_app/app/middleware.php」(修正した)の中身
|
12
104
|
|
13
105
|
|
14
106
|
|
@@ -70,9 +162,7 @@
|
|
70
162
|
|
71
163
|
___________________________________________________________
|
72
164
|
|
73
|
-
(2)
|
74
|
-
|
75
|
-
my_slim_app/app/routes.php の中身
|
165
|
+
(5)「my_slim_app/app/routes.php」 (修正した)の中身
|
76
166
|
|
77
167
|
|
78
168
|
|
@@ -128,9 +218,7 @@
|
|
128
218
|
|
129
219
|
___________________________________________________________
|
130
220
|
|
131
|
-
(3)
|
132
|
-
|
133
|
-
my_slim_app/public/index.php
|
221
|
+
(6)「my_slim_app/public/index.php」(本のままの無修正)の中身
|
134
222
|
|
135
223
|
|
136
224
|
|
3
回答の順序を整理しました。
test
CHANGED
@@ -1,25 +1,287 @@
|
|
1
|
-
###
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
### 「Slim、TWI」に関係するドキュメントを追加。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
以下が、関係するファイル群です。
|
6
|
+
|
7
|
+
___________________________________________________________
|
8
|
+
|
9
|
+
(1)
|
10
|
+
|
11
|
+
my_slim_app/app/middleware.php の中身
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
<?php
|
18
|
+
|
19
|
+
declare(strict_types=1);
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
use DI\Container;
|
24
|
+
|
25
|
+
use Slim\Factory\AppFactory;
|
26
|
+
|
27
|
+
use Slim\Views\Twig;
|
28
|
+
|
29
|
+
use Slim\Views\TwigMiddleware;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
return function ($app) {
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
// Create Container
|
38
|
+
|
39
|
+
$container = new Container();
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
// create App
|
44
|
+
|
45
|
+
AppFactory::setContainer($container);
|
46
|
+
|
47
|
+
$app = AppFactory::create();
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
// Set view in Container
|
52
|
+
|
53
|
+
$container->set('view', function() {
|
54
|
+
|
55
|
+
return Twig::create('../templates', ['cache' => '../templates/cache']);
|
56
|
+
|
57
|
+
});
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// Add Twig-View Middleware
|
62
|
+
|
63
|
+
$app->add(TwigMiddleware::createFromContainer($app));
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
};
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
___________________________________________________________
|
72
|
+
|
73
|
+
(2)
|
74
|
+
|
75
|
+
my_slim_app/app/routes.php の中身
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
<?php
|
82
|
+
|
83
|
+
declare(strict_types=1);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
use App\Application\Actions\User\ListUsersAction;
|
88
|
+
|
89
|
+
use App\Application\Actions\User\ViewUserAction;
|
90
|
+
|
91
|
+
use Psr\Http\Message\ResponseInterface as Response;
|
92
|
+
|
93
|
+
use Psr\Http\Message\ServerRequestInterface as Request;
|
94
|
+
|
95
|
+
use Slim\App;
|
96
|
+
|
97
|
+
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
return function ($app) {
|
102
|
+
|
103
|
+
$app->get('/', function ($request, $response) {
|
104
|
+
|
105
|
+
$response->getBody()->write('Hello 2 world!');
|
106
|
+
|
107
|
+
return $response;
|
108
|
+
|
109
|
+
});
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
$app->get('/{name}', function ($request, $response, $args) {
|
114
|
+
|
115
|
+
return $this->get('view')->render($response, 'index.html', [
|
116
|
+
|
117
|
+
'name' => $args['name']
|
118
|
+
|
119
|
+
]);
|
120
|
+
|
121
|
+
});
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
};
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
___________________________________________________________
|
130
|
+
|
131
|
+
(3)
|
132
|
+
|
133
|
+
my_slim_app/public/index.php の中身
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
<?php
|
140
|
+
|
141
|
+
declare(strict_types=1);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
use App\Application\Handlers\HttpErrorHandler;
|
146
|
+
|
147
|
+
use App\Application\Handlers\ShutdownHandler;
|
148
|
+
|
149
|
+
use App\Application\ResponseEmitter\ResponseEmitter;
|
150
|
+
|
151
|
+
use DI\ContainerBuilder;
|
152
|
+
|
153
|
+
use Slim\Factory\AppFactory;
|
154
|
+
|
155
|
+
use Slim\Factory\ServerRequestCreatorFactory;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
require __DIR__ . '/../vendor/autoload.php';
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// Instantiate PHP-DI ContainerBuilder
|
164
|
+
|
165
|
+
$containerBuilder = new ContainerBuilder();
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
if (false) { // Should be set to true in production
|
170
|
+
|
171
|
+
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// Set up settings
|
178
|
+
|
179
|
+
$settings = require __DIR__ . '/../app/settings.php';
|
180
|
+
|
181
|
+
$settings($containerBuilder);
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// Set up dependencies
|
186
|
+
|
187
|
+
$dependencies = require __DIR__ . '/../app/dependencies.php';
|
188
|
+
|
189
|
+
$dependencies($containerBuilder);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// Set up repositories
|
194
|
+
|
195
|
+
$repositories = require __DIR__ . '/../app/repositories.php';
|
196
|
+
|
197
|
+
$repositories($containerBuilder);
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
// Build PHP-DI Container instance
|
202
|
+
|
203
|
+
$container = $containerBuilder->build();
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
// Instantiate the app
|
208
|
+
|
209
|
+
AppFactory::setContainer($container);
|
210
|
+
|
211
|
+
$app = AppFactory::create();
|
212
|
+
|
213
|
+
$callableResolver = $app->getCallableResolver();
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
// Register middleware
|
218
|
+
|
219
|
+
$middleware = require __DIR__ . '/../app/middleware.php';
|
220
|
+
|
221
|
+
$middleware($app);
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// Register routes
|
226
|
+
|
227
|
+
$routes = require __DIR__ . '/../app/routes.php';
|
228
|
+
|
229
|
+
$routes($app);
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
/** @var bool $displayErrorDetails */
|
234
|
+
|
235
|
+
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// Create Request object from globals
|
240
|
+
|
241
|
+
$serverRequestCreator = ServerRequestCreatorFactory::create();
|
242
|
+
|
243
|
+
$request = $serverRequestCreator->createServerRequestFromGlobals();
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// Create Error Handler
|
248
|
+
|
249
|
+
$responseFactory = $app->getResponseFactory();
|
250
|
+
|
251
|
+
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// Create Shutdown Handler
|
256
|
+
|
257
|
+
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
|
258
|
+
|
259
|
+
register_shutdown_function($shutdownHandler);
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// Add Routing Middleware
|
264
|
+
|
265
|
+
$app->addRoutingMiddleware();
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// Add Error Middleware
|
270
|
+
|
271
|
+
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
|
272
|
+
|
273
|
+
$errorMiddleware->setDefaultErrorHandler($errorHandler);
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// Run App & Emit Response
|
278
|
+
|
279
|
+
$response = $app->handle($request);
|
280
|
+
|
281
|
+
$responseEmitter = new ResponseEmitter();
|
282
|
+
|
283
|
+
$responseEmitter->emit($response);
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
```
|
2
誤字脱字の訂正です。
test
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
実は、「Web開発のためのMySQL」の本で
|
5
|
+
実は、購入した「Web開発のためのMySQL」の本にて学習中です。
|
6
6
|
|
7
|
+
PHP、MySQL、フレームワークを昨年の暮れから、年末年始を利用し、
|
8
|
+
|
7
|
-
|
9
|
+
学習中でして、同じところで躓いていました。
|
8
10
|
|
9
11
|
|
10
12
|
|
1
誤字脱字の訂正です。
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
### PHP
|
1
|
+
### PHPを学習中です。
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -8,14 +8,16 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
11
|
+
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
12
|
+
|
13
|
+
「Eggpanさん」の回答をヒントに、先ほど(昨晩)1時間あまりで、動き出しました。
|
12
14
|
|
13
15
|
|
14
16
|
|
15
|
-
まっさらな状態から、フ
|
17
|
+
まっさらな状態から、フォルダー作成(my_slim)インストール(composer)して、
|
16
18
|
|
17
19
|
動き出しています。
|
18
20
|
|
19
21
|
|
20
22
|
|
21
|
-
それが、先ほどの回答です
|
23
|
+
それが、先ほどの回答ですが、何か、環境が違うとだめかもしれませんね!
|