- Symfony 教程 (jiàochéng)
- Symfony - 首页 (shǒuyè)
- Symfony - 简介 (jiànjiè)
- Symfony - 安装 (ānzhuāng)
- Symfony - 架构 (jiàgòu)
- Symfony - 组件 (zǔjiàn)
- Symfony - 服务容器 (fúwù róngqì)
- Symfony - 事件 & 事件监听器 (shìjiàn & shìjiàn tīngzhōngqì)
- Symfony - 表达式 (biǎodáshì)
- Symfony - 捆绑包 (kuǎnbǎng bāo)
- 创建简单的 Web 应用 (chuàngjiàn jiǎndān de Web yìngyòng)
- Symfony - 控制器 (kòngzhìqì)
- Symfony - 路由 (lùyóu)
- Symfony - 视图引擎 (shìtú yǐnqíng)
- Symfony - Doctrine ORM
- Symfony - 表单 (biǎodān)
- Symfony - 验证 (yànzhèng)
- Symfony - 文件上传 (wénjiàn shàngchuān)
- Symfony - Ajax 控制 (kòngzhì)
- Cookies & 会话管理 (huìhuà guǎnlǐ)
- Symfony - 国际化 (guójì huà)
- Symfony - 日志 (rìzhì)
- Symfony - 邮件管理 (yóujiàn guǎnlǐ)
- Symfony - 单元测试 (dānyuán cèshì)
- Symfony - 高级概念 (gāojí gàiniàn)
- Symfony - REST 版本 (bǎnběn)
- Symfony - CMF 版本 (bǎnběn)
- 完整的可运行示例 (wánzhěng de kě yùnxíng shìlì)
- Symfony 有用资源 (yǒuyòng zīyuán)
- Symfony - 快速指南 (kuàisù zhǐnàn)
- Symfony - 有用资源 (yǒuyòng zīyuán)
- Symfony - 讨论 (tǎolùn)
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 :).