symfony/framework-bundle
Symfony FrameworkBundle tightly integrates Symfony components into the full-stack framework, providing core framework services and configuration. Part of the main Symfony repository; see official docs for contributing, issues, and pull requests.
Installation:
composer require symfony/framework-bundle
This package is typically included automatically when using symfony/symfony or symfony/flex.
First Use Case:
use Symfony\Component\HttpKernel\KernelInterface;
public function __construct(private KernelInterface $kernel) {}
config/routes.yaml or via annotations/attributes:
# config/routes.yaml
app_homepage:
path: /
controller: App\Controller\HomeController::index
Where to Look First:
config/packages/framework.yaml (auto-generated by Symfony Flex).config/bundles.php (auto-configured).Service Configuration:
framework.yaml to configure core features like session, cache, or HTTP cache:
# config/packages/framework.yaml
framework:
session:
handler_id: cache.app
http_cache:
invalidation:
enabled: true
config/packages/dev/framework.yaml).Dependency Injection:
RequestStack, RouterInterface).kernel.event_listener):
#[Tag('kernel.event_listener', ['event' => 'kernel.request'])]
class MyListener {}
Routing and Controllers:
#[Route('/profile', name: 'app_profile')]
public function profile(): Response {}
@Route to attributes using useAttributeAsKey in framework.yaml:
framework:
router:
use_attribute_as_key: true
Testing:
Symfony\Bundle\FrameworkBundle\Test\KernelTestCase:
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MyTest extends KernelTestCase {
public function testSomething(): void {
self::bootKernel();
$client = static::createClient();
$client->request('GET', '/');
}
}
self::$container to access services in tests.Event Listeners/Subscribers:
services.yaml:
services:
App\EventListener\MyListener:
tags: ['kernel.event_listener']
tags: ['kernel.event_subscriber']
Console Commands:
src/Command/ and tag them:
#[AsCommand(name: 'app:greet')]
class GreetCommand extends Command {}
services.yaml:
tags: ['console.command']
Configuration Reference:
php bin/console config:dump-reference framework
BundleInterface to create custom bundles with FrameworkBundle features.%env(APP_SECRET)% in framework.yaml or .env files..env (APP_DEBUG=1) and use the profiler (/_profiler).Caching Quirks:
php bin/console cache:warmup after config changes. In tests, use self::bootKernel() to ensure a fresh container.KernelTestCase).Routing Issues:
use_attribute_as_key: true is set for attribute routing to work.YAML routes over annotations/attributes if conflicts arise.Dependency Injection:
autowire: false and explicit binding if needed.services.yaml.Session Handling:
MockSessionStorage in tests:
$session = new Session();
$session->setStorage(new MockSessionStorage());
self::$container->set('session', $session);
cookie_lifetime is set in framework.yaml for session cookies.Configuration Merging:
config/packages/overrides.yaml.config:dump-reference to debug config shapes.Testing Gotchas:
KernelTestCase:
self::ensureKernelShutdown();
Console Commands:
symfony/console is installed if using commands.console package is present.php bin/console debug:container
php bin/console debug:router
php bin/console debug:env
php bin/console debug:config framework
php bin/console debug:event-dispatcher
ConfiguratorInterface for custom config validation/normalization.EventSubscriberInterface for kernel events (e.g., kernel.request).HttpKernelInterface for custom request/response handling.CompilerPass to modify the container at compile time.LoaderInterface implementations for dynamic routes.cache.app for session storage to reduce database load.http_cache for static assets:
framework:
http_cache:
enabled: true
framework.yaml:
framework:
csrf_protection:
enabled: true
framework.yaml:
framework:
http_client:
headers:
Strict-Transport-Security: "max-age=31536000; includeSubDomains"
nelmio/cors-bundle for cross-origin requests.How can I help you explore Laravel packages today?