Files
yii2/docs/guide-ja/structure-application-components.md
Carsten Brandt 6f12a14328 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
2015-01-27 01:46:33 +09:00

8.4 KiB

アプリケーションコンポーネント

アプリケーションは サービスロケータ です。 アプリケーションは、リクエストを処理するためのいろいろなサービスを提供する一連のオブジェクト、いわゆる アプリケーションコンポーネント をホストします。 例えば、urlManager がウェブリクエストを適切なコントローラにルーティングする役割を負い、db コンポーネントが DB 関連のサービスを提供する、等々です。

全てのアプリケーションコンポーネントは、それぞれ、同一のアプリケーション内で他のアプリケーションコンポーネントから区別できるように、ユニークな ID を持ちます。 アプリケーションコンポーネントには、次の式によってアクセス出来ます。

\Yii::$app->componentID

例えば、\Yii::$app->db を使って、アプリケーションに登録された yii\db\Connection を取得することが出来ます。 また、\Yii::$app->cache を使って、yii\caching\Cache を取得できます。

アプリケーションコンポーネントは、上記の式を使ってアクセスされた最初の時に作成されます。 二度目以降のアクセスでは、同じコンポーネントインスタンスが返されます。

どのようなオブジェクトでも、アプリケーションコンポーネントとすることが可能です。 アプリケーションの構成情報 の中で yii\base\Application::components プロパティを構成することによって、アプリケーションコンポーネントを登録することが出来ます。 例えば、

[
    '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|情報: 必要なだけ多くのアプリケーションコンポーネントを登録することが出来ますが、慎重にしなければなりません。 アプリケーションコンポーネントはグローバル変数のようなものです。 あまり多くのアプリケーションコンポーネントを使うと、コードのテストと保守が困難になるおそれがあります。 多くの場合、必要なときにローカルなコンポーネントを作成して使用するだけで十分です。

コンポーネントをブートストラップに含める

上述のように、アプリケーションコンポーネントは最初にアクセスされた時に初めてインスタンスが作成されます。 リクエストの間に全くアクセスされなかった時は、インスタンスは作成されません。 けれども、場合によっては、明示的にアクセスされないときでも、リクエストごとにアプリケーションコンポーネントのインスタンスを作成したいことがあります。 そうするためには、アプリケーションの yii\base\Application::bootstrap プロパティのリストにそのコンポーネントの ID を挙げることが出来ます。

例えば、次のアプリケーション構成情報は、log コンポーネントが常にロードされることを保証するものです。

[
    'bootstrap' => [
        'log',
    ],
    'components' => [
        'log' => [
            // "log" コンポーネントの構成情報
        ],
    ],
]

コアアプリケーションコンポーネント

Yii は固定の ID とデフォルトの構成情報を持つ一連の コア アプリケーションコンポーネントを定義しています。 例えば、yii\web\Application::request コンポーネントは、ユーザリクエストに関する情報を収集して、それを ルート として解決するために使用されます。 また、yii\base\Application::db コンポーネントは、それを通じてデータベースクエリを実行できるデータベース接続を表現します。 Yii のアプリケーションがユーザリクエストを処理出来るのは、まさにこれらのコアアプリケーションコンポーネントの助けがあってこそです。

下記が事前に定義されたコアアプリケーションコンポーネントです。 通常のアプリケーションコンポーネントに対するのと同様に、これらを構成し、カスタマイズすることが出来ます。 コアアプリケーションコンポーネントを構成するときは、クラスを指定しない場合は、デフォルトのクラスが使用されます。