vendor/ 並更新 composer.json/composer.lock。AppKernel.php 啟用 Bundle。config.yml 等。codeguy/upload、doctrine/doctrine-fixtures-bundle。# 自動選擇相容版本
composer require vendor/package
# 指定版本
composer require vendor/package:^1.2
# 開發環境專用
composer require --dev vendor/package
<?php // app/AppKernel.php
public function registerBundles()
{
$bundles = [
// Symfony 核心 Bundles...
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
// ...
// 第三方 Bundles
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
// 新安裝的 Bundle 加在這裡
];
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
// ...
}
return $bundles;
}
注意:部分 Bundle 需區分環境(如僅 dev/test 載入),請參考其文件。
# 列出所有 Bundle 設定參考
php app/console config:dump-reference
# 查看特定 Bundle(如 TwigBundle)
php app/console config:dump-reference TwigBundle
# Default configuration for "TwigBundle"
twig:
exception_controller: 'twig.controller.exception:showAction'
form_themes: ['form_div_layout.html.twig']
globals:
site_name: 'My Site'
cache: '%kernel.cache_dir%/twig'
debug: '%kernel.debug%'
# ...
# app/config/config.yml
stof_doctrine_extensions:
orm:
default:
timestampable: true
sluggable: true
softdeleteable: true
| Bundle | Composer 指令 | Kernel 類別 | 是否需額外設定 |
|---|---|---|---|
| DoctrineFixturesBundle | composer require --dev doctrine/doctrine-fixtures-bundle | Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle() | 否 |
| StofDoctrineExtensionsBundle | composer require stof/doctrine-extensions-bundle | Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle() | 是 (config.yml) |
| FOSUserBundle | composer require friendsofsymfony/user-bundle | FOS\UserBundle\FOSUserBundle() | 是 (security.yml, config.yml) |
| KnpPaginatorBundle | composer require knplabs/knp-paginator-bundle | Knp\Bundle\PaginatorBundle\KnpPaginatorBundle() | 否(可選) |
| LiipImagineBundle | composer require liip/imagine-bundle | Liip\ImagineBundle\LiipImagineBundle() | 是 (config.yml) |
php app/console debug:container --parameter=kernel.bundles
# 或
php app/console debug:config VendorBundleName
php app/console cache:clear。composer.json 的 require 區塊,手動調整版本約束後 composer update。遵循此標準流程,即能安全、可維護地擴展 Symfony 專案功能。