このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
slim:4:routing [2020/10/10 17:51] y2sunlight [Registering a controller with the container] |
slim:4:routing [2020/10/10 21:27] (現在) y2sunlight [インボーク可能なクラスの使用] |
||
---|---|---|---|
行 493: | 行 493: | ||
==== コントローラーをコンテナーに登録する ==== | ==== コントローラーをコンテナーに登録する ==== | ||
- | Create a controller with the '' | + | '' |
- | + | ||
- | '' | + | |
<code php> | <code php> | ||
行 523: | 行 521: | ||
} | } | ||
</ | </ | ||
- | |||
- | Create a factory in the container that instantiates the controller with the dependencies: | ||
依存関係を持つコントローラーをインスタンス化するファクトリをコンテナーに作成します。 | 依存関係を持つコントローラーをインスタンス化するファクトリをコンテナーに作成します。 | ||
行 541: | 行 537: | ||
}); | }); | ||
</ | </ | ||
- | |||
- | This allows you to leverage the container for dependency injection and so you can inject specific dependencies into the controller. | ||
これにより、依存関係の注入にコンテナーを活用できるため、特定の依存関係をコントローラーに注入できます。 | これにより、依存関係の注入にコンテナーを活用できるため、特定の依存関係をコントローラーに注入できます。 | ||
行 548: | 行 542: | ||
\\ | \\ | ||
- | ==== Allow Slim to instantiate the controller | + | ==== Slimがコントローラーをインスタンス化できるようにする |
- | Alternatively, | + | また、クラスがコンテナにエントリされていない場合、Slimはコンテナのインスタンスをそのクラスのコンストラクタに渡します。1つのアクションのみを処理する呼び出し可能なクラスの代わりに、多くのアクションを持つコントローラーを構築できるのです。 |
- | + | ||
- | または、クラスのコンテナにエントリがない場合、Slimはコンテナのインスタンスをコンストラクタに渡します。 | + | |
<code php> | <code php> | ||
行 587: | 行 579: | ||
</ | </ | ||
- | You can use your controller methods like so. | + | このコントローラーメソッドは次のように使用できます。 |
- | + | ||
- | このようにコントローラーメソッドを使用できます。 | + | |
<code php> | <code php> | ||
行 598: | 行 588: | ||
\\ | \\ | ||
- | ==== Using an invokable class ==== | + | ==== インボーク可能なアクションクラスの使用 |
- | You do not have to specify a method in your route callable and can just set it to be an invokable class such as: | + | ルート呼び出しのメソッドを指定する必要はなく、次のようなインボーク可能なアクションクラスとして設定するだけです: |
- | + | ||
- | ルート呼び出し可能メソッドでメソッドを指定する必要はなく、次のような呼び出し可能クラスに設定するだけです: | + | |
<code php> | <code php> | ||
行 629: | 行 617: | ||
</ | </ | ||
- | You can use this class like so. | + | このクラスは次のように使用できます。 |
- | + | ||
- | このクラスはこのように使用できます。 | + | |
<code php> | <code php> | ||
行 637: | 行 623: | ||
</ | </ | ||
- | Again, as with controllers, | + | 繰り返しになりますが、コントローラーを使った場合と同様に、クラス名をコンテナーに登録すると、ファクトリを作成して、アクションクラスに必要としている特定の依存関係だけを注入できます。 |
- | + | ||
- | 繰り返しますが、コントローラーの場合と同様に、クラス名をコンテナーに登録すると、ファクトリを作成して、必要な特定の依存関係だけをアクションクラスに挿入できます。 | + | |
\\ | \\ | ||