回答編集履歴
5
回答の順序を整理しました。
test
CHANGED
@@ -1,287 +1,49 @@
|
|
1
|
-
###
|
1
|
+
### PHPを学習中です。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
実は、購入した**「Web開発のためのMySQL」**の本にて学習中です。
|
6
|
+
|
7
|
+
PHP、MySQL、フレームワークを昨年の暮れから、年末年始を利用し、
|
8
|
+
|
5
|
-
|
9
|
+
学習中でして、同じところで躓いていました。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
14
|
+
|
15
|
+
「Eggpanさん」の回答をヒントに、先ほど(昨晩)1時間あまりで、動き出しました。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
まっさらな状態から、フォルダー作成(my_slim)インストール(composer)して、
|
20
|
+
|
21
|
+
動き出しています。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
それが、先ほどの回答ですが、何か、環境が違うとだめかもしれませんね!
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
-
|
6
30
|
|
7
31
|
___________________________________________________________
|
8
32
|
|
9
|
-
|
33
|
+
-
|
10
34
|
|
11
|
-
|
35
|
+
その前の回答は、
|
12
36
|
|
37
|
+
-
|
13
38
|
|
39
|
+
**もしかして、この順も、大事! 逆は、だめかも? **
|
40
|
+
|
41
|
+
-
|
14
42
|
|
15
43
|
```
|
16
44
|
|
17
|
-
|
45
|
+
composer require php-di/php-di
|
18
46
|
|
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
|
-
|
47
|
+
composer require slim/twig-view
|
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
48
|
|
69
49
|
```
|
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
|
-
```
|
4
誤字脱字の訂正です。
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
### 「Slim、TWI」に関係するドキュメントを追加
|
1
|
+
### 「Slim、TWI」に関係するドキュメントを追加。
|
2
2
|
|
3
3
|
|
4
4
|
|
3
誤字脱字の訂正です。
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
### 「Slim、TWI」関係
|
1
|
+
### 「Slim、TWI」に関係するドキュメントを追加しました。
|
2
2
|
|
3
3
|
|
4
4
|
|
2
誤字脱字の修正です。
test
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
### 関係のドキュメントを追加しました。
|
1
|
+
### 「Slim、TWI」関係のドキュメントを追加しました。
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
-
以下が、関係
|
5
|
+
以下が、関係するファイル群です。
|
6
6
|
|
7
7
|
___________________________________________________________
|
8
8
|
|
1
追加しました。
test
CHANGED
@@ -1,15 +1,287 @@
|
|
1
|
-
###
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
```
|
1
|
+
### 関係のドキュメントを追加しました。
|
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
|
+
```
|