このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
slim:4:concepts [2020/10/02 11:50] y2sunlight [ミドルウェアを追加するにはどうすればよい?] |
slim:4:concepts [2020/10/07 21:19] y2sunlight |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > 編集中 | + | ====== Slim4 コンセプト ====== |
- | + | ||
- | ---- | + | |
- | + | ||
- | ====== Slim4 のコンセプト ====== | + | |
Version 4.5.0 | Version 4.5.0 | ||
- | --- // | + | --- // |
[[slim: | [[slim: | ||
行 12: | 行 8: | ||
関連記事 | 関連記事 | ||
- | * [[slim: | + | * [[slim: |
- | * Slim4のコンセプト | + | * Slim4 コンセプト |
- | * [[slim:4:basic|Slim4の基本的な使い方]] | + | * [[slim:4:request|Slim4 |
+ | * [[slim: | ||
+ | * [[slim: | ||
+ | * [[slim: | ||
+ | * [[slim: | ||
- | 本章は以下のサイトの | + | 本章は以下のサイトの **Concepts** のセクションを翻訳し若干の補足を加えたのもです。 |
* https:// | * https:// | ||
行 416: | 行 416: | ||
===== 依存性コンテナ― ===== | ===== 依存性コンテナ― ===== | ||
- | >TODO | + | |
+ | Slimは、オプションの依存性コンテナーを使用して、アプリケーションの依存関係を準備、管理、および注入します。Slimは、[[https:// | ||
+ | |||
+ | > PSR-11の邦訳は[[psr: | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== PHP-DIでの使用例 ==== | ||
+ | |||
+ | 依存関係コンテナを必ず提供する必要があるとは限りません。但し、そうする場合は、'' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | use DI\Container; | ||
+ | use Psr\Http\Message\ResponseInterface as Response; | ||
+ | use Psr\Http\Message\ServerRequestInterface as Request; | ||
+ | use Slim\Factory\AppFactory; | ||
+ | |||
+ | require __DIR__ . '/ | ||
+ | |||
+ | // Create Container using PHP-DI | ||
+ | $container = new Container(); | ||
+ | |||
+ | // Set container to create App with on AppFactory | ||
+ | AppFactory:: | ||
+ | $app = AppFactory:: | ||
+ | </ | ||
+ | |||
+ | そして、コンテナにサービスを追加します: | ||
+ | |||
+ | <code php> | ||
+ | $container-> | ||
+ | $settings = [...]; | ||
+ | return new MyService($settings); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | 次のように、明示的にコンテナからからだけでなく、Slimアプリケーションルートの内からサービスをフェッチできます: | ||
+ | |||
+ | <code php> | ||
+ | /** | ||
+ | * Example GET route | ||
+ | * | ||
+ | * @param | ||
+ | * @param | ||
+ | * @param | ||
+ | * | ||
+ | * @return ResponseInterface | ||
+ | */ | ||
+ | $app-> | ||
+ | $myService = $this-> | ||
+ | |||
+ | // ...do something with $myService... | ||
+ | |||
+ | return $response; | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | コンテナを使用する前にサービスがコンテナに存在するかどうかをテストするには、次のように '' | ||
+ | |||
+ | <code php> | ||
+ | /** | ||
+ | * Example GET route | ||
+ | * | ||
+ | * @param | ||
+ | * @param | ||
+ | * @param | ||
+ | * | ||
+ | * @return ResponseInterface | ||
+ | */ | ||
+ | $app-> | ||
+ | if ($this-> | ||
+ | $myService = $this-> | ||
+ | } | ||
+ | return $response; | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== コンテナを介したアプリケーションの構成 ==== | ||
+ | |||
+ | コンテナに依存性が既に定義されている '' | ||
+ | |||
+ | **例題** | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | use App\Factory\MyResponseFactory; | ||
+ | use DI\Container; | ||
+ | use Psr\Container\ContainerInterface; | ||
+ | use Psr\Http\Message\ResponseFactoryInterface; | ||
+ | use Slim\Factory\AppFactory; | ||
+ | |||
+ | require_once __DIR__ . '/ | ||
+ | |||
+ | // Create Container using PHP-DI | ||
+ | $container = new Container(); | ||
+ | |||
+ | // Add custom response factory | ||
+ | $container-> | ||
+ | return new MyResponseFactory(...); | ||
+ | }); | ||
+ | |||
+ | // Configure the application via container | ||
+ | $app = AppFactory:: | ||
+ | |||
+ | // ... | ||
+ | |||
+ | $app-> | ||
+ | </ | ||
+ | |||
+ | サポートされているアプリの依存関係は次のとおりです: | ||
+ | |||
+ | * Psr\Http\Message\ResponseFactoryInterface | ||
+ | * Slim\Interfaces\CallableResolverInterface | ||
+ | * Slim\Interfaces\RouteCollectorInterface | ||
+ | * Slim\Interfaces\RouteResolverInterface | ||
+ | * Slim\Interfaces\MiddlewareDispatcherInterface | ||
\\ | \\ | ||