Symfony-bundles

提供:Dev Guides
移動先:案内検索

Symfony-バンドル

Symfonyバンドルは、特定の構造に編成されたファイルとフォルダーのコレクションです。 バンドルは、複数のアプリケーションで再利用できるようにモデル化されています。 メインアプリケーション自体はバンドルとしてパッケージ化されており、通常は AppBundle と呼ばれます。

バンドルは、AdminBundle(管理セクション)、BlogBu​​ndle(サイトのブログ)などのアプリケーションに固有にパッケージ化できます。 このようなバンドルは、アプリケーション間で共有できません。 代わりに、ブログなどのアプリケーションの特定の部分を汎用バンドルとしてモデル化して、あるアプリケーションから別のアプリケーションにバンドルを単純にコピーして、ブログ機能を再利用できます。

バンドルの構造

バンドルの基本構造は次のとおりです。

  • コントローラー-すべてのコントローラーをここに配置する必要があります。
  • DependencyInjection -依存性注入に関連するすべてのコードと構成をここに配置する必要があります。
  • Resources/config -バンドル関連の設定はここに配置されます。
  • Resources/view -バンドル関連のビューテンプレートはここに配置されます。
  • リソース/パブリック-バンドル関連のスタイルシート、JavaScript、画像などがここに配置されます。
  • テスト-バンドル関連のユニットテストファイルはここに配置されます。

バンドルを作成する

*HelloWorld* アプリケーションで簡単なバンドル *finddevguidesDemoBundle* を作成してみましょう。

ステップ1 *-名前空間を選択します。 バンドルのネームスペースには、ベンダー名とバンドル名を含める必要があります。 この例では、 *finddevguides \ DemoBundle です。

ステップ2 *- *Bundle クラスを拡張して空のクラス finddevguidesDemoBundle を作成し、 src/finddevguides/DemoBundle の下に配置します。

namespace finddevguides\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class finddevguidesDemoBundle extends Bundle {
}

ステップ3 *-アプリケーションがサポートするバンドルのリストにクラスを *AppKernel クラスに登録します。

public function registerBundles() {
   $bundles = array(
     //...
     //register your bundle
      new finddevguides\DemoBundle\finddevguidesDemoBundle(),
   );
   return $bundles;
}

これは、空のバンドルを作成するためにすべて必要であり、他のすべての概念はアプリケーションの概念と同じです。 また、symfonyはコンソールコマンド generate:bundle を提供して、新しいバンドルを作成するプロセスを簡素化します。これは次のとおりです。

php bin/console generate:bundle --namespace = finddevguides/DemoBundle

結果

Welcome to the Symfony bundle generator!

Are you planning on sharing this bundle across multiple applications? [no]: no

Your application code must be written in bundles. This command helps
you generate them easily.

Give your bundle a descriptive name, like BlogBundle.
Bundle name [finddevguides/DemoBundle]:

In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest finddevguidesDemoBundle.

Bundle name [finddevguidesDemoBundle]:
Bundles are usually generated into the src/directory. Unless you're
doing something custom, hit enter to keep this default!
Target Directory [src/]:

What format do you want to use for your generated configuration?

Configuration format (annotation, yml, xml, php) [annotation]:

Bundle generation

> Generating a sample bundle skeleton into app/../src/finddevguides/DemoBundle
   created ./app/../src/finddevguides/DemoBundle/
   created ./app/../src/finddevguides/DemoBundle/finddevguidesDemoBundle.php
   created ./app/../src/finddevguides/DemoBundle/Controller/
   created ./app/../src/finddevguides/DemoBundle/Controller/DefaultController.php
   created ./app/../tests/finddevguidesDemoBundle/Controller/
   created ./app/../tests/finddevguidesDemoBundle/Controller/DefaultControllerTest.php
   created ./app/../src/finddevguides/DemoBundle/Resources/views/Default/
   created ./app/../src/finddevguides/DemoBundle/Resources/views/Default/indexl.twig
   created ./app/../src/finddevguides/DemoBundle/Resources/config/
   created ./app/../src/finddevguides/DemoBundle/Resources/config/services.yml
> Checking that the bundle is autoloaded
> Enabling the bundle inside app/AppKernel.php
   updated ./app/AppKernel.php
> Importing the bundle's routes from the app/config/routing.yml file
   updated ./app/config/routing.yml
> Importing the bundle's services.yml from the app/config/config.yml file
   updated ./app/config/config.yml
Everything is OK! Now get to work :).