Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
9.0 KiB

アプリケーションコンポーネント
==============================
アプリケーションは [サービスロケータ](concept-service-locator.md) です。
アプリケーションは、リクエストを処理するためのいろいろなサービスを提供する一連のオブジェクト、いわゆる *アプリケーションコンポーネント* をホストします。
例えば、`urlManager` がウェブリクエストを適切なコントローラにルーティングする役割を負い、`db` コンポーネントが DB 関連のサービスを提供する、等々です。
全てのアプリケーションコンポーネントは、それぞれ、同一のアプリケーション内で他のアプリケーションコンポーネントから区別できるように、ユニークな ID を持ちます。
アプリケーションコンポーネントには、次の式によってアクセス出来ます。
```php
\Yii::$app->componentID
```
例えば、`\Yii::$app->db` を使って、アプリケーションに登録された [[yii\db\Connection|DB 接続]] を取得することが出来ます。
また、`\Yii::$app->cache` を使って、[[yii\caching\Cache|プライマリキャッシュ]] を取得できます。
アプリケーションコンポーネントは、上記の式を使ってアクセスされた最初の時に作成されます。
二度目以降のアクセスでは、同じコンポーネントインスタンスが返されます。
どのようなオブジェクトでも、アプリケーションコンポーネントとすることが可能です。
[アプリケーションの構成情報](structure-applications.md#application-configurations) の中で [[yii\base\Application::components]] プロパティを構成することによって、アプリケーションコンポーネントを登録することが出来ます。
例えば、
```php
[
'components' => [
// クラス名を使って "cache" コンポーネントを登録
'cache' => 'yii\caching\ApcCache',
// 構成情報の配列を使って "db" コンポーネントを登録
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=demo',
'username' => 'root',
'password' => '',
],
// 無名関数を使って "search" コンポーネントを登録
'search' => function () {
return new app\components\SolrService;
},
],
]
```
> Info: 必要なだけ多くのアプリケーションコンポーネントを登録することが出来ますが、慎重にしなければなりません。
アプリケーションコンポーネントはグローバル変数のようなものです。
あまり多くのアプリケーションコンポーネントを使うと、コードのテストと保守が困難になるおそれがあります。
多くの場合、必要なときにローカルなコンポーネントを作成して使用するだけで十分です。
## コンポーネントをブートストラップに含める <span id="bootstrapping-components"></span>
上述のように、アプリケーションコンポーネントは最初にアクセスされた時に初めてインスタンスが作成されます。
リクエストの間に全くアクセスされなかった時は、インスタンスは作成されません。
けれども、場合によっては、明示的にアクセスされないときでも、リクエストごとにアプリケーションコンポーネントのインスタンスを作成したいことがあります。
そうするために、アプリケーションの [[yii\base\Application::bootstrap|bootstrap]] プロパティのリストにそのコンポーネントの ID を挙げることが出来ます。
また、特別なコンポーネントをブートストラップするためにクロージャを用いることも出来ます。
インスタンス化されたコンポーネントを返すことは要求されません。
単に [[yii\base\Application]] のインスタンス化の後にコードを走らせるだけのためにクロージャを使うことも出来ます。
例えば、次のアプリケーション構成情報は、`log` コンポーネントが常にロードされることを保証するものです。
```php
[
'bootstrap' => [
'log',
function($app){
return new ComponentX();
},
function($app){
// 何らかのコード
return;
}
],
'components' => [
'log' => [
// "log" コンポーネントの構成情報
],
],
]
```
use id instead of name for anchor references in the guide fixes #7013 Conflicts: docs/guide-es/caching-data.md docs/guide-es/caching-fragment.md docs/guide-es/caching-http.md docs/guide-es/concept-aliases.md docs/guide-es/concept-autoloading.md docs/guide-es/concept-behaviors.md docs/guide-es/concept-configurations.md docs/guide-es/concept-di-container.md docs/guide-es/concept-events.md docs/guide-es/db-dao.md docs/guide-es/helper-array.md docs/guide-es/helper-html.md docs/guide-es/helper-overview.md docs/guide-es/rest-authentication.md docs/guide-es/rest-controllers.md docs/guide-es/rest-error-handling.md docs/guide-es/rest-quick-start.md docs/guide-es/rest-resources.md docs/guide-es/rest-response-formatting.md docs/guide-es/runtime-handling-errors.md docs/guide-es/runtime-logging.md docs/guide-es/runtime-requests.md docs/guide-es/runtime-responses.md docs/guide-es/runtime-routing.md docs/guide-es/runtime-sessions-cookies.md docs/guide-es/start-databases.md docs/guide-es/start-forms.md docs/guide-es/start-gii.md docs/guide-es/start-hello.md docs/guide-es/start-installation.md docs/guide-es/start-workflow.md docs/guide-es/structure-application-components.md docs/guide-es/structure-applications.md docs/guide-es/structure-assets.md docs/guide-es/structure-controllers.md docs/guide-es/structure-entry-scripts.md docs/guide-es/structure-extensions.md docs/guide-es/structure-filters.md docs/guide-es/structure-models.md docs/guide-es/structure-modules.md docs/guide-es/structure-views.md docs/guide-es/structure-widgets.md docs/guide-es/tutorial-core-validators.md docs/guide-es/tutorial-yii-integration.md docs/guide-fr/start-databases.md docs/guide-fr/start-forms.md docs/guide-fr/start-gii.md docs/guide-fr/start-hello.md docs/guide-fr/start-installation.md docs/guide-fr/start-workflow.md docs/guide-fr/structure-entry-scripts.md docs/guide-it/start-installation.md docs/guide-ja/structure-application-components.md docs/guide-ja/structure-applications.md docs/guide-ja/structure-controllers.md docs/guide-pt-BR/start-databases.md docs/guide-pt-BR/start-forms.md docs/guide-pt-BR/start-gii.md docs/guide-pt-BR/start-hello.md docs/guide-pt-BR/start-installation.md docs/guide-pt-BR/start-workflow.md docs/guide-pt-BR/structure-application-components.md docs/guide-pt-BR/structure-applications.md docs/guide-pt-BR/structure-assets.md docs/guide-pt-BR/structure-controllers.md docs/guide-pt-BR/structure-entry-scripts.md docs/guide-pt-BR/structure-extensions.md docs/guide-pt-BR/structure-filters.md docs/guide-pt-BR/structure-models.md docs/guide-pt-BR/structure-modules.md docs/guide-pt-BR/structure-views.md docs/guide-pt-BR/structure-widgets.md docs/guide-ru/caching-data.md docs/guide-ru/caching-fragment.md docs/guide-ru/caching-http.md docs/guide-ru/concept-aliases.md docs/guide-ru/concept-autoloading.md docs/guide-ru/concept-behaviors.md docs/guide-ru/concept-configurations.md docs/guide-ru/concept-di-container.md docs/guide-ru/concept-events.md docs/guide-ru/input-validation.md docs/guide-ru/rest-authentication.md docs/guide-ru/rest-controllers.md docs/guide-ru/rest-quick-start.md docs/guide-ru/rest-resources.md docs/guide-ru/rest-response-formatting.md docs/guide-ru/runtime-requests.md docs/guide-ru/runtime-responses.md docs/guide-ru/start-databases.md docs/guide-ru/start-forms.md docs/guide-ru/start-gii.md docs/guide-ru/start-hello.md docs/guide-ru/start-installation.md docs/guide-ru/start-workflow.md docs/guide-ru/structure-application-components.md docs/guide-ru/structure-applications.md docs/guide-ru/structure-controllers.md docs/guide-ru/structure-entry-scripts.md docs/guide-ru/structure-extensions.md docs/guide-ru/structure-filters.md docs/guide-ru/structure-modules.md docs/guide-ru/structure-views.md docs/guide-ru/structure-widgets.md docs/guide-ru/tutorial-core-validators.md docs/guide-ru/tutorial-i18n.md docs/guide-ru/tutorial-yii-integration.md docs/guide-uk/caching-fragment.md docs/guide-uk/concept-aliases.md docs/guide-uk/concept-autoloading.md docs/guide-uk/start-databases.md docs/guide-uk/start-forms.md docs/guide-uk/start-gii.md docs/guide-uk/start-hello.md docs/guide-uk/start-installation.md docs/guide-uk/start-workflow.md docs/guide-uk/structure-application-components.md docs/guide-uk/structure-applications.md docs/guide-uk/structure-controllers.md docs/guide-uk/structure-entry-scripts.md docs/guide-zh-CN/caching-data.md docs/guide-zh-CN/caching-fragment.md docs/guide-zh-CN/caching-http.md docs/guide-zh-CN/concept-aliases.md docs/guide-zh-CN/concept-autoloading.md docs/guide-zh-CN/concept-configurations.md docs/guide-zh-CN/concept-di-container.md docs/guide-zh-CN/input-validation.md docs/guide-zh-CN/output-formatter.md docs/guide-zh-CN/rest-quick-start.md docs/guide-zh-CN/rest-response-formatting.md docs/guide-zh-CN/runtime-handling-errors.md docs/guide-zh-CN/runtime-responses.md docs/guide-zh-CN/runtime-routing.md docs/guide-zh-CN/runtime-sessions-cookies.md docs/guide-zh-CN/start-databases.md docs/guide-zh-CN/start-forms.md docs/guide-zh-CN/start-gii.md docs/guide-zh-CN/start-hello.md docs/guide-zh-CN/start-installation.md docs/guide-zh-CN/start-workflow.md docs/guide-zh-CN/structure-application-components.md docs/guide-zh-CN/structure-applications.md docs/guide-zh-CN/structure-assets.md docs/guide-zh-CN/structure-controllers.md docs/guide-zh-CN/structure-entry-scripts.md docs/guide-zh-CN/structure-extensions.md docs/guide-zh-CN/structure-filters.md docs/guide-zh-CN/structure-models.md docs/guide-zh-CN/structure-modules.md docs/guide-zh-CN/structure-views.md docs/guide-zh-CN/structure-widgets.md docs/guide-zh-CN/tutorial-core-validators.md docs/guide-zh-CN/tutorial-yii-integration.md extensions/apidoc/helpers/ApiMarkdown.php extensions/apidoc/templates/html/views/constSummary.php extensions/apidoc/templates/html/views/type.php Conflicts: docs/guide-ja/structure-application-components.md
10 years ago
## コアアプリケーションコンポーネント <span id="core-application-components"></span>
Yii は固定の ID とデフォルトの構成情報を持つ一連の *コア* アプリケーションコンポーネントを定義しています。
例えば、[[yii\web\Application::request|request]] コンポーネントは、ユーザリクエストに関する情報を収集して、それを [ルート](runtime-routing.md) として解決するために使用されます。
また、[[yii\base\Application::db|db]] コンポーネントは、それを通じてデータベースクエリを実行できるデータベース接続を表現します。
Yii のアプリケーションがユーザリクエストを処理出来るのは、まさにこれらのコアアプリケーションコンポーネントの助けがあってこそです。
下記が事前に定義されたコアアプリケーションコンポーネントです。
通常のアプリケーションコンポーネントに対するのと同様に、これらを構成し、カスタマイズすることが出来ます。
コアアプリケーションコンポーネントを構成するときは、クラスを指定しない場合は、デフォルトのクラスが使用されます。
* [[yii\web\AssetManager|assetManager]]: アセットバンドルとアセットの発行を管理します。
詳細は [アセット](structure-assets.md) の節を参照してください。
* [[yii\db\Connection|db]]: データベース接続を表します。これを通じて、DB クエリを実行することが出来ます。
このコンポーネントを構成するときは、コンポーネントのクラスはもちろん、[[yii\db\Connection::dsn]] のような必須のコンポーネントプロパティを指定しなければならないことに注意してください。
詳細は [データアクセスオブジェクト](db-dao.md) の節を参照してください。
* [[yii\base\Application::errorHandler|errorHandler]]: PHP のエラーと例外を処理します。
詳細は [エラー処理](runtime-handling-errors.md) の節を参照してください。
* [[yii\i18n\Formatter|formatter]]: エンドユーザに表示されるデータに書式を設定します。
例えば、数字が3桁ごとの区切りを使って表示されたり、日付が長い書式で表示されたりします。
詳細は [データの書式設定](output-formatting.md) の節を参照してください。
* [[yii\i18n\I18N|i18n]]: メッセージの翻訳と書式設定をサポートします。
詳細は [国際化](tutorial-i18n.md) の節を参照してください。
* [[yii\log\Dispatcher|log]]: ログターゲットを管理します。
詳細は [ロギング](runtime-logging.md) の節を参照してください。
* [[yii\swiftmailer\Mailer|mailer]]: メールの作成と送信をサポートします。
詳細は [メール](tutorial-mailing.md) の節を参照してください。
* [[yii\base\Application::response|response]]: エンドユーザに送信されるレスポンスを表現します。
詳細は [レスポンス](runtime-responses.md) の節を参照してください。
* [[yii\base\Application::request|request]]: エンドユーザから受信したリクエストを表現します。
詳細は [リクエスト](runtime-requests.md) の節を参照してください。
* [[yii\web\Session|session]]: セッション情報を表現します。
このコンポーネントは、[[yii\web\Application|ウェブアプリケーション]] においてのみ利用できます。
詳細は [セッションとクッキー](runtime-sessions-cookies.md) の節を参照してください。
* [[yii\web\UrlManager|urlManager]]: URL の解析と生成をサポートします。
詳細は [ルーティング と URL 生成](runtime-routing.md) の節を参照してください。
* [[yii\web\User|user]]: ユーザの認証情報を表現します。
このコンポーネントは、[[yii\web\Application|ウェブアプリケーション]] においてのみ利用できます。
詳細は [認証](security-authentication.md) の節を参照してください。
* [[yii\web\View|view]]: ビューのレンダリングをサポートします。
詳細は [ビュー](structure-views.md) の節を参照してください。