Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


slim:4:concepts

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
最新のリビジョン 両方とも次のリビジョン
slim:4:concepts [2020/10/02 09:50]
y2sunlight [ミドルウェアを追加するにはどうすればよい?]
slim:4:concepts [2020/10/05 16:50]
y2sunlight
行 1: 行 1:
-> 編集中 +====== Slim4 コンセプト ======
- +
----- +
- +
-====== Slim4 コンセプト ======+
 Version 4.5.0 Version 4.5.0
  
- --- //[[http://www.y2sunlight.com|y2sunlight]] 2020-03-05//+ --- //[[http://www.y2sunlight.com|y2sunlight]] 2020-09-23//
  
 [[slim:top|Slim に戻る]] [[slim:top|Slim に戻る]]
行 12: 行 8:
 関連記事 関連記事
  
-  * [[slim:4:install|Slim4インストール]] +  * [[slim:4:install|Slim4 インストール]] 
-  * Slim4コンセプト +  * Slim4 コンセプト 
-  * [[slim:4:basic|Slim4の基本的な使い方]]+  * [[slim:4:request|Slim4 リクエスト]] 
 +  * [[slim:4:response|Slim4 レスポンス]] 
 +  * [[slim:4:routing|Slim4 ルーティング]] 
 +  * [[slim:4:middleware|Slim4 ミドルウェア]]
  
-本章は以下のサイトの ''**Concepts**'' のセクションを翻訳し若干の補足を加えたのもです。+本章は以下のサイトの **Concepts** のセクションを翻訳し若干の補足を加えたのもです。
  
   * https://www.slimframework.com/docs/v4/   * https://www.slimframework.com/docs/v4/
行 287: 行 286:
  
 === ルートミドルウェア === === ルートミドルウェア ===
- 
-Add middleware to a Route with the Route instance’s add() method. This example adds the Closure middleware example above: 
  
 ルートミドルウェアは、そのルートが現在のHTTPリクエストメソッドとURIに一致する場合にのみインボークされます。ルートミドルウェアは、Slimアプリケーションのルーティングメソッド(''get()'' や ''post()'' など)を呼び出した直後に指定されます。各ルーティングメソッドは ''\Slim\Route'' のインスタンスを返し、このクラスはSlimアプリケーションインスタンスと同じミドルウェアインターフェイスを提供します。''Route'' インスタンスの ''add()'' メソッドを使用して、ミドルウェアを ''Route'' に追加します。この例では、上記のクロージャミドルウェアの例を追加します。 ルートミドルウェアは、そのルートが現在のHTTPリクエストメソッドとURIに一致する場合にのみインボークされます。ルートミドルウェアは、Slimアプリケーションのルーティングメソッド(''get()'' や ''post()'' など)を呼び出した直後に指定されます。各ルーティングメソッドは ''\Slim\Route'' のインスタンスを返し、このクラスはSlimアプリケーションインスタンスと同じミドルウェアインターフェイスを提供します。''Route'' インスタンスの ''add()'' メソッドを使用して、ミドルウェアを ''Route'' に追加します。この例では、上記のクロージャミドルウェアの例を追加します。
行 319: 行 316:
 </code> </code>
  
-This would output this HTTP response body: +これにより、次のHTTPレスポンスボディーが出力されます
- +
-これにより、次のHTTP応答本文が出力されます+
  
 <code> <code>
行 327: 行 322:
 </code> </code>
  
-=== Group middleware === +=== グループミドルウェア ===
- +
-In addition to the overall application, and standard routes being able to accept middleware, the group() multi-route definition functionality, also allows individual routes internally. Route group middleware is invoked only if its route matches one of the defined HTTP request methods and URIs from the group. To add middleware within the callback, and entire-group middleware to be set by chaining add() after the group() method. +
- +
-アプリケーション全体、およびミドルウェアを受け入れることができる標準ルートに加えて、group()マルチルート定義機能は、内部で個々のルートも許可します。 ルートグループミドルウェアは、そのルートがグループから定義されたHTTP要求メソッドおよびURIの1つと一致する場合にのみ呼び出されます。 コールバック内にミドルウェアを追加し、group()メソッドの後にadd()をチェーンすることによって設定されるグループ全体のミドルウェア。+
  
-Sample Application, making use of callback middleware on a group of url-handlers+アプリケーション全体、及びミドルウェアを受け入れることができる標準的なルート(route)に加えて、''group()'' マルチルート定義機能も、内部的に個別のルート(route)に許可されています。ルートグループミドルウェアは、そのルートがグループから定義されたHTTPリクエストメソッド及びURIの1つと一致する場合にのみ呼び出されます。コールバック内でミドルウェアを追加するには、''group()'' メソッドの後に ''add()'' をチェーンすることによってグループ全体のミドルウェアを設定します。
  
-URLハンドラーのグループでコールバックミドルウェアを利用するサンプルアプリケーション+以下は、URLハンドラーのグループでコールバックミドルウェアを利用するサンプルアプリケーションです。
  
 <code php> <code php>
行 377: 行 368:
 </code> </code>
  
-When calling the /utils/date method, this would output a string similar to the below +''/utils/date'' メソッドを呼び出すと、次のような文字列が出力されます。
- +
-/ utils / dateメソッドを呼び出すと、次のような文字列が出力されます。+
  
 <code> <code>
行 385: 行 374:
 </code> </code>
  
-Visiting /utils/time would output a string similar to the below +''/utils/time'' にアクセスすると、次のような文字列が出力されます。
- +
-/ utils / timeにアクセスすると、次のような文字列が出力されます。+
  
 <code> <code>
行 393: 行 380:
 </code> </code>
  
-But visiting / (domain-root), would be expected to generate the following output as no middleware has been assigned +ただし、''/''(domain-root)にアクセスすると、ミドルウェアが割り当てられていないため、次の出力が生成されると思います。
- +
-ただし、/(domain-root)にアクセスすると、ミドルウェアが割り当てられていないため、次の出力が生成されると予想されます。+
  
 <code> <code>
行 401: 行 386:
 </code> </code>
  
-=== Passing variables from middleware ===+=== ミドルウェアから変数を渡す ===
  
-The easiest way to pass attributes from middleware is to use the request’s attributes. +ミドルウェアから変数を渡す最も簡単な方法は、リクエストの属性を使用することです。
- +
-ミドルウェアから属性を渡す最も簡単な方法は、リクエストの属性を使用することです。 +
- +
-Setting the variable in the middleware:+
  
 ミドルウェアでの変数の設定: ミドルウェアでの変数の設定:
行 415: 行 396:
 </code> </code>
  
-Getting the variable in the route callback: +その変数をルートコールバックで取得する:
- +
-ルートコールバックで変数を取得する:+
  
 <code php> <code php>
行 436: 行 415:
  
 ===== 依存性コンテナ― ===== ===== 依存性コンテナ― =====
->TODO+ 
 +Slimは、オプションの依存性コンテナーを使用して、アプリケーションの依存関係を準備、管理、および注入します。Slimは、[[https://php-di.org/doc/frameworks/slim.html|PHP-DI]] のような [[https://www.php-fig.org/psr/psr-11/|PSR-11]] を実装するコンテナーをサポートします。 
 + 
 +PSR-11の邦訳は[[psr:psr11|こちら]]にあります。 
 + 
 +\\ 
 + 
 +==== PHP-DIでの使用例 ==== 
 + 
 +依存関係コンテナを必ず提供する必要があるとは限りません。但し、そうする場合は、''App'' を作成する前に、コンテナのインスタンスを ''AppFactory'' に与える必要があります。 
 + 
 +<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__ . '/../vendor/autoload.php'; 
 + 
 +// Create Container using PHP-DI 
 +$container = new Container(); 
 + 
 +// Set container to create App with on AppFactory 
 +AppFactory::setContainer($container); 
 +$app = AppFactory::create(); 
 +</code> 
 + 
 +そして、コンテナにサービスを追加します: 
 + 
 +<code php> 
 +$container->set('myService', function () { 
 +    $settings = [...]; 
 +    return new MyService($settings); 
 +}); 
 +</code> 
 + 
 +次のように、明示的にコンテナからからだけでなく、Slimアプリケーションルートの内からサービスをフェッチできます: 
 + 
 +<code php> 
 +/** 
 + * Example GET route 
 + * 
 + * @param  ServerRequestInterface $request  PSR-7 request 
 + * @param  ResponseInterface      $response  PSR-7 response 
 + * @param  array                  $args Route parameters 
 + * 
 + * @return ResponseInterface 
 + */ 
 +$app->get('/foo', function (Request $request, Response $response, $args) { 
 +    $myService = $this->get('myService'); 
 + 
 +    // ...do something with $myService... 
 + 
 +    return $response; 
 +}); 
 +</code> 
 + 
 +コンテナを使用する前にサービスがコンテナに存在するかどうかをテストするには、次のように ''has()'' メソッドを使用します: 
 + 
 +<code php> 
 +/** 
 + * Example GET route 
 + * 
 + * @param  ServerRequestInterface $request  PSR-7 request 
 + * @param  ResponseInterface      $response  PSR-7 response 
 + * @param  array                  $args Route parameters 
 + * 
 + * @return ResponseInterface 
 + */ 
 +$app->get('/foo', function (Request $request, Response $response, $args) { 
 +    if ($this->has('myService')) { 
 +        $myService = $this->get('myService'); 
 +    } 
 +    return $response; 
 +}); 
 +</code> 
 + 
 +\\ 
 + 
 +==== コンテナを介したアプリケーションの構成 ==== 
 + 
 +コンテナに依存性が既に定義されている ''App'' を作成する場合は、''AppFactory::createFromContainer()'' メソッドが使用できます。 
 + 
 +**例題** 
 + 
 +<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__ . '/../vendor/autoload.php'; 
 + 
 +// Create Container using PHP-DI 
 +$container = new Container(); 
 + 
 +// Add custom response factory 
 +$container->set(ResponseFactoryInterface::class, function (ContainerInterface $container) { 
 +    return new MyResponseFactory(...); 
 +}); 
 + 
 +// Configure the application via container 
 +$app = AppFactory::createFromContainer($container); 
 + 
 +// ... 
 + 
 +$app->run(); 
 +</code> 
 + 
 +サポートされているアプリの依存関係は次のとおりです: 
 + 
 +  * Psr\Http\Message\ResponseFactoryInterface 
 +  * Slim\Interfaces\CallableResolverInterface 
 +  * Slim\Interfaces\RouteCollectorInterface 
 +  * Slim\Interfaces\RouteResolverInterface 
 +  * Slim\Interfaces\MiddlewareDispatcherInterface
  
 \\ \\
  
  
slim/4/concepts.txt · 最終更新: 2020/10/07 21:19 by y2sunlight