Symfony - 捆綁包 (kuǎnbǎng bāo)



Symfony 捆綁包是一個以特定結構組織的檔案和資料夾集合。捆綁包的設計使其能夠在多個應用程式中重複使用。主應用程式本身也被打包成一個捆綁包,通常稱為AppBundle

捆綁包可以針對特定應用程式進行打包,例如 AdminBundle(管理部分)、BlogBundle(網站部落格)等。此類捆綁包不能在應用程式之間共享。相反,我們可以將應用程式的某些部分(例如部落格)建模為通用捆綁包,這樣我們就可以簡單地將捆綁包從一個應用程式複製到另一個應用程式以重用部落格功能。

捆綁包的結構

捆綁包的基本結構如下。

  • Controller − 所有控制器都需要放置在此處。

  • DependencyInjection − 所有與依賴注入相關的程式碼和配置都需要放置在此處。

  • Resources/config − 捆綁包相關的配置放置在此處。

  • Resources/view − 捆綁包相關的檢視模板放置在此處。

  • Resources/public − 捆綁包相關的樣式表、JavaScript、影像等放置在此處。

  • Tests − 捆綁包相關的單元測試檔案放置在此處。

建立捆綁包

讓我們在 HelloWorld 應用程式中建立一個簡單的捆綁包,TutorialspointDemoBundle

步驟 1 − 選擇名稱空間。捆綁包的名稱空間應包含供應商名稱和捆綁包名稱。在本例中,它是 Tutorialspoint\DemoBundle

步驟 2 − 透過擴充套件 Bundle 類建立一個空類 TutorialspointDemoBundle,並將其放置在 src/Tutorialspoint/DemoBundle 下。

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

class TutorialspointDemoBundle extends Bundle { 
}

步驟 3 − 在 AppKernel 類中,將該類註冊到應用程式支援的捆綁包列表中。

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

建立空捆綁包只需要這些步驟,所有其他概念與應用程式相同。Symfony 還提供了一個控制檯命令 generate:bundle 來簡化建立新捆綁包的過程,如下所示。

php bin/console generate:bundle --namespace = Tutorialspoint/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 [Tutorialspoint/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 TutorialspointDemoBundle.  

Bundle name [TutorialspointDemoBundle]:  
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/Tutorialspoint/DemoBundle 
   created ./app/../src/Tutorialspoint/DemoBundle/ 
   created ./app/../src/Tutorialspoint/DemoBundle/TutorialspointDemoBundle.php 
   created ./app/../src/Tutorialspoint/DemoBundle/Controller/ 
   created ./app/../src/Tutorialspoint/DemoBundle/Controller/DefaultController.php 
   created ./app/../tests/TutorialspointDemoBundle/Controller/   
   created ./app/../tests/TutorialspointDemoBundle/Controller/DefaultControllerTest.php 
   created ./app/../src/Tutorialspoint/DemoBundle/Resources/views/Default/   
   created ./app/../src/Tutorialspoint/DemoBundle/Resources/views/Default/index.html.twig 
   created ./app/../src/Tutorialspoint/DemoBundle/Resources/config/ 
   created ./app/../src/Tutorialspoint/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 :).  
廣告 (guǎnggào)