このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
apricot:usage:ja:provider [2020/08/29 16:55] y2sunlight [DIコンテナの使用例] |
apricot:usage:ja:provider [2020/09/03 13:46] (現在) y2sunlight [Apricot サービスプロバイダー] |
||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | > 編集中 | ||
| - | |||
| - | ---- | ||
| - | |||
| ====== Apricot サービスプロバイダー ====== | ====== Apricot サービスプロバイダー ====== | ||
| --- // | --- // | ||
| 行 21: | 行 17: | ||
| * [[apricot: | * [[apricot: | ||
| * Apricot サービスプロバイダー | * Apricot サービスプロバイダー | ||
| + | * [[apricot: | ||
| * [[apricot: | * [[apricot: | ||
| ---- | ---- | ||
| - | Apricotでは、DIコンテナーに [[https:// | + | Apricotでは、DIコンテナーに [[https:// |
| - | + | ||
| - | Apricotのスケルトンでは、サービスプロバイターの簡単な例題を提供しています。 | + | |
| \\ | \\ | ||
| 行 82: | 行 77: | ||
| | | ||
| ; | ; | ||
| - | |||
| $this-> | $this-> | ||
| } | } | ||
| 行 90: | 行 84: | ||
| スケルトンでは[[apricot: | スケルトンでは[[apricot: | ||
| - | '' | + | '' |
| サービス(またはモデル)をサービスプロバイターに登録する場合は、上例に習って適宜追加して下さい。League/ | サービス(またはモデル)をサービスプロバイターに登録する場合は、上例に習って適宜追加して下さい。League/ | ||
| 行 96: | 行 90: | ||
| \\ | \\ | ||
| - | ===== App\Foundation\Containerクラス ===== | + | ===== Containerクラス ===== |
| - | サービスプロバイダーで登録してサービス(またはモデル) | + | サービスプロバイダーで登録したサービス(またはモデル)を取得するには、'' |
| - | + | ||
| - | App\Foundation\Containerクラスは、\League\Container\Container | + | |
| - | + | ||
| - | サービスの使用者は、'' | + | |
| 使用法: ** Container:: | 使用法: ** Container:: | ||
| 行 109: | 行 99: | ||
| |mixed get(string $id)|識別子idでコンテナのエントリを検索して返します。| | |mixed get(string $id)|識別子idでコンテナのエントリを検索して返します。| | ||
| |bool has(string $id)|コンテナが指定された識別子idのエントリを返すことができる場合はtrueを返します。| | |bool has(string $id)|コンテナが指定された識別子idのエントリを返すことができる場合はtrueを返します。| | ||
| - | |||
| - | {{fa> | ||
| - | <code php Container.php> | ||
| - | <?php | ||
| - | namespace App\Foundation; | ||
| - | |||
| - | use Apricot\Foundation\Singleton; | ||
| - | use App\Provider; | ||
| - | |||
| - | /** | ||
| - | * Container class for service | ||
| - | * | ||
| - | * @method static Container getInstance() Gets the Container instance. | ||
| - | * @method static mixed get(string $id) Finds an entry of the container by its identifier and returns it. | ||
| - | * @method static bool has(string $id) Returns true if the container can return an entry for the given identifier. | ||
| - | */ | ||
| - | class Container extends Singleton | ||
| - | { | ||
| - | /** | ||
| - | * Create Container instance. | ||
| - | * @return \League\Container\Container | ||
| - | */ | ||
| - | protected static function createInstance() | ||
| - | { | ||
| - | $container = new \League\Container\Container; | ||
| - | $container-> | ||
| - | return $container; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| \\ | \\ | ||
| 行 144: | 行 104: | ||
| ===== 使用例 ===== | ===== 使用例 ===== | ||
| - | === スタブコントローラ | + | スタブコントローラのindexアクションには、DIコンテナの使用例があります。 |
| - | DIコンテナをテストするために、スタブコントローラを以下のように修正します。 | + | {{fa> |
| - | + | ||
| - | {{fa> | + | |
| <code php StubController.php> | <code php StubController.php> | ||
| - | <?php | + | public function index(int $no=null) |
| - | namespace App\Controllers; | + | { |
| + | $title = "Stub {$no}"; | ||
| - | use App\Foundation\Container; | ||
| - | use App\Foundation\Controller; | ||
| - | |||
| - | /** | ||
| - | * Stub Controller | ||
| - | */ | ||
| - | class StubController extends Controller | ||
| - | { | ||
| /** | /** | ||
| - | * Index Page for this controller. | + | * @var \App\Services\SampleService $service |
| - | * | + | |
| - | * @return | + | |
| */ | */ | ||
| - | | + | $service |
| - | { | + | $count = $service->getUserCount(); |
| - | $title = "Stub {$no}"; | + | $messages[] = " |
| - | + | ||
| - | /* | + | |
| - | * Example for Container | + | |
| - | * @var \App\Models\User $user | + | |
| - | */ | + | |
| - | | + | |
| - | $userCount | + | |
| - | $messages[] = " | + | |
| - | | + | |
| - | } | + | |
| } | } | ||
| </ | </ | ||
| - | * '' | + | '' |
| - | * ユーザモデルの '' | + | |
| - | * ユーザ数を表示するために、テンプレート変数 '' | + | |
| \\ | \\ | ||