回答編集履歴
4
具体的な例を記述しています。
answer
CHANGED
@@ -1,11 +1,57 @@
|
|
1
1
|
### 「Slim、TWI」に関係するドキュメントを追加。
|
2
2
|
|
3
|
-
以下が、
|
3
|
+
以下が、パソコン環境です。
|
4
|
+
|
5
|
+
PHP 7.3.12
|
6
|
+
Slim/Slim 4.4
|
7
|
+
Slim/psr7 1.0
|
8
|
+
PHP-di/PHP-di 6.0
|
9
|
+
Slim/twig-view 3.0
|
10
|
+
|
11
|
+
さらに、関係するドキュメントを記述しています。
|
12
|
+
|
4
13
|
___________________________________________________________
|
5
|
-
(1)
|
6
|
-
|
14
|
+
(1)本に書かれたままで、「http://localhost:8080/」を実行した結果
|
7
15
|
|
8
16
|
```
|
17
|
+
Fatal error: Uncaught TypeError:
|
18
|
+
|
19
|
+
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
|
20
|
+
|
21
|
+
called in C:\Users\xxxx\Desktop\my_slim_app\app\middleware.php on line 19
|
22
|
+
|
23
|
+
and defined in C:\Users\xxxx\Desktop\my_slim_app\vendor\slim\twig-view\src\Twig.php:104
|
24
|
+
Stack trace: #0
|
25
|
+
C:\Users\xxxx\Desktop\my_slim_app\app\middleware.php(19): Slim\Views\Twig->__construct('../templates', Array) #1
|
26
|
+
C:\Users\xxxx\Desktop\my_slim_app\public\index.php(44): {closure}(Object(Slim\App)) #2
|
27
|
+
|
28
|
+
{main} thrown in C:\Users\xxxx\Desktop\my_slim_app\vendor\slim\twig-view\src\Twig.php on line 104
|
29
|
+
```
|
30
|
+
|
31
|
+
___________________________________________________________
|
32
|
+
(2)そのため、(4)と(5)の修正をして、「http://localhost:8080/」を実行した結果
|
33
|
+
|
34
|
+
```
|
35
|
+
Hello 2 world!
|
36
|
+
```
|
37
|
+
|
38
|
+
___________________________________________________________
|
39
|
+
(3)さらに、(4)と(5)の修正のままで、「http://localhost:8080/HANAKO」を実行した結果
|
40
|
+
|
41
|
+
```
|
42
|
+
{
|
43
|
+
"statusCode": 500,
|
44
|
+
"error": {
|
45
|
+
"type": "SERVER_ERROR",
|
46
|
+
"description": "No entry or class found for 'view'"
|
47
|
+
}
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
___________________________________________________________
|
52
|
+
(4)「my_slim_app/app/middleware.php」(修正した)の中身
|
53
|
+
|
54
|
+
```
|
9
55
|
<?php
|
10
56
|
declare(strict_types=1);
|
11
57
|
|
@@ -34,8 +80,7 @@
|
|
34
80
|
};
|
35
81
|
```
|
36
82
|
___________________________________________________________
|
37
|
-
(2)
|
38
|
-
my_slim_app/app/routes.php の中身
|
83
|
+
(5)「my_slim_app/app/routes.php」 (修正した)の中身
|
39
84
|
|
40
85
|
```
|
41
86
|
<?php
|
@@ -63,8 +108,7 @@
|
|
63
108
|
};
|
64
109
|
```
|
65
110
|
___________________________________________________________
|
66
|
-
(3)
|
67
|
-
my_slim_app/public/index.php
|
111
|
+
(6)「my_slim_app/public/index.php」(本のままの無修正)の中身
|
68
112
|
|
69
113
|
```
|
70
114
|
<?php
|
3
回答の順序を整理しました。
answer
CHANGED
@@ -1,13 +1,144 @@
|
|
1
|
-
###
|
1
|
+
### 「Slim、TWI」に関係するドキュメントを追加。
|
2
2
|
|
3
|
-
実は、購入した「Web開発のためのMySQL」の本にて学習中です。
|
4
|
-
PHP、MySQL、フレームワークを昨年の暮れから、年末年始を利用し、
|
5
|
-
|
3
|
+
以下が、関係するファイル群です。
|
4
|
+
___________________________________________________________
|
5
|
+
(1)
|
6
|
+
my_slim_app/app/middleware.php の中身
|
6
7
|
|
8
|
+
```
|
9
|
+
<?php
|
7
|
-
|
10
|
+
declare(strict_types=1);
|
8
|
-
「Eggpanさん」の回答をヒントに、先ほど(昨晩)1時間あまりで、動き出しました。
|
9
11
|
|
12
|
+
use DI\Container;
|
13
|
+
use Slim\Factory\AppFactory;
|
14
|
+
use Slim\Views\Twig;
|
10
|
-
|
15
|
+
use Slim\Views\TwigMiddleware;
|
11
|
-
動き出しています。
|
12
16
|
|
17
|
+
return function ($app) {
|
18
|
+
|
19
|
+
// Create Container
|
20
|
+
$container = new Container();
|
21
|
+
|
22
|
+
// create App
|
23
|
+
AppFactory::setContainer($container);
|
24
|
+
$app = AppFactory::create();
|
25
|
+
|
13
|
-
|
26
|
+
// Set view in Container
|
27
|
+
$container->set('view', function() {
|
28
|
+
return Twig::create('../templates', ['cache' => '../templates/cache']);
|
29
|
+
});
|
30
|
+
|
31
|
+
// Add Twig-View Middleware
|
32
|
+
$app->add(TwigMiddleware::createFromContainer($app));
|
33
|
+
|
34
|
+
};
|
35
|
+
```
|
36
|
+
___________________________________________________________
|
37
|
+
(2)
|
38
|
+
my_slim_app/app/routes.php の中身
|
39
|
+
|
40
|
+
```
|
41
|
+
<?php
|
42
|
+
declare(strict_types=1);
|
43
|
+
|
44
|
+
use App\Application\Actions\User\ListUsersAction;
|
45
|
+
use App\Application\Actions\User\ViewUserAction;
|
46
|
+
use Psr\Http\Message\ResponseInterface as Response;
|
47
|
+
use Psr\Http\Message\ServerRequestInterface as Request;
|
48
|
+
use Slim\App;
|
49
|
+
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
|
50
|
+
|
51
|
+
return function ($app) {
|
52
|
+
$app->get('/', function ($request, $response) {
|
53
|
+
$response->getBody()->write('Hello 2 world!');
|
54
|
+
return $response;
|
55
|
+
});
|
56
|
+
|
57
|
+
$app->get('/{name}', function ($request, $response, $args) {
|
58
|
+
return $this->get('view')->render($response, 'index.html', [
|
59
|
+
'name' => $args['name']
|
60
|
+
]);
|
61
|
+
});
|
62
|
+
|
63
|
+
};
|
64
|
+
```
|
65
|
+
___________________________________________________________
|
66
|
+
(3)
|
67
|
+
my_slim_app/public/index.php の中身
|
68
|
+
|
69
|
+
```
|
70
|
+
<?php
|
71
|
+
declare(strict_types=1);
|
72
|
+
|
73
|
+
use App\Application\Handlers\HttpErrorHandler;
|
74
|
+
use App\Application\Handlers\ShutdownHandler;
|
75
|
+
use App\Application\ResponseEmitter\ResponseEmitter;
|
76
|
+
use DI\ContainerBuilder;
|
77
|
+
use Slim\Factory\AppFactory;
|
78
|
+
use Slim\Factory\ServerRequestCreatorFactory;
|
79
|
+
|
80
|
+
require __DIR__ . '/../vendor/autoload.php';
|
81
|
+
|
82
|
+
// Instantiate PHP-DI ContainerBuilder
|
83
|
+
$containerBuilder = new ContainerBuilder();
|
84
|
+
|
85
|
+
if (false) { // Should be set to true in production
|
86
|
+
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
|
87
|
+
}
|
88
|
+
|
89
|
+
// Set up settings
|
90
|
+
$settings = require __DIR__ . '/../app/settings.php';
|
91
|
+
$settings($containerBuilder);
|
92
|
+
|
93
|
+
// Set up dependencies
|
94
|
+
$dependencies = require __DIR__ . '/../app/dependencies.php';
|
95
|
+
$dependencies($containerBuilder);
|
96
|
+
|
97
|
+
// Set up repositories
|
98
|
+
$repositories = require __DIR__ . '/../app/repositories.php';
|
99
|
+
$repositories($containerBuilder);
|
100
|
+
|
101
|
+
// Build PHP-DI Container instance
|
102
|
+
$container = $containerBuilder->build();
|
103
|
+
|
104
|
+
// Instantiate the app
|
105
|
+
AppFactory::setContainer($container);
|
106
|
+
$app = AppFactory::create();
|
107
|
+
$callableResolver = $app->getCallableResolver();
|
108
|
+
|
109
|
+
// Register middleware
|
110
|
+
$middleware = require __DIR__ . '/../app/middleware.php';
|
111
|
+
$middleware($app);
|
112
|
+
|
113
|
+
// Register routes
|
114
|
+
$routes = require __DIR__ . '/../app/routes.php';
|
115
|
+
$routes($app);
|
116
|
+
|
117
|
+
/** @var bool $displayErrorDetails */
|
118
|
+
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];
|
119
|
+
|
120
|
+
// Create Request object from globals
|
121
|
+
$serverRequestCreator = ServerRequestCreatorFactory::create();
|
122
|
+
$request = $serverRequestCreator->createServerRequestFromGlobals();
|
123
|
+
|
124
|
+
// Create Error Handler
|
125
|
+
$responseFactory = $app->getResponseFactory();
|
126
|
+
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
|
127
|
+
|
128
|
+
// Create Shutdown Handler
|
129
|
+
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
|
130
|
+
register_shutdown_function($shutdownHandler);
|
131
|
+
|
132
|
+
// Add Routing Middleware
|
133
|
+
$app->addRoutingMiddleware();
|
134
|
+
|
135
|
+
// Add Error Middleware
|
136
|
+
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
|
137
|
+
$errorMiddleware->setDefaultErrorHandler($errorHandler);
|
138
|
+
|
139
|
+
// Run App & Emit Response
|
140
|
+
$response = $app->handle($request);
|
141
|
+
$responseEmitter = new ResponseEmitter();
|
142
|
+
$responseEmitter->emit($response);
|
143
|
+
|
144
|
+
```
|
2
誤字脱字の訂正です。
answer
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
### PHPを学習中です。
|
2
2
|
|
3
|
-
実は、「Web開発のためのMySQL」の本で
|
3
|
+
実は、購入した「Web開発のためのMySQL」の本にて学習中です。
|
4
|
+
PHP、MySQL、フレームワークを昨年の暮れから、年末年始を利用し、
|
4
|
-
|
5
|
+
学習中でして、同じところで躓いていました。
|
5
6
|
|
6
7
|
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
7
8
|
「Eggpanさん」の回答をヒントに、先ほど(昨晩)1時間あまりで、動き出しました。
|
1
誤字脱字の訂正です。
answer
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
### PHP
|
1
|
+
### PHPを学習中です。
|
2
2
|
|
3
3
|
実は、「Web開発のためのMySQL」の本で、PHP、MySQL、フレームワークを
|
4
4
|
昨年の暮れから、年末年始を利用して、学習中でして、同じところで躓いていました。
|
5
5
|
|
6
|
-
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
6
|
+
そこで、3時間前にたまたま「TwigMiddleware」で検索して、この「質問」にぶち当たり、
|
7
|
+
「Eggpanさん」の回答をヒントに、先ほど(昨晩)1時間あまりで、動き出しました。
|
7
8
|
|
8
|
-
まっさらな状態から、フ
|
9
|
+
まっさらな状態から、フォルダー作成(my_slim)インストール(composer)して、
|
9
10
|
動き出しています。
|
10
11
|
|
11
|
-
それが、先ほどの回答です
|
12
|
+
それが、先ほどの回答ですが、何か、環境が違うとだめかもしれませんね!
|