symfony/framework-bundle
FrameworkBundle integrates Symfony components into the full-stack Symfony framework, providing core wiring for services, configuration, routing, controllers, and more. Part of the main Symfony repository; see docs for contributing, issues, and PRs.
Installation:
composer require symfony/framework-bundle
This is typically included by default in new Symfony projects via symfony/symfony meta-package.
First Use Case:
config/packages/framework.yaml to adjust core framework behavior (e.g., session, router, HTTP cache).
framework:
router:
default_uri: 'https://example.com' # Base URL for generated links
session:
cookie_lifetime: 3600 # Session cookie lifetime in seconds
config/routes.yaml or via annotations/attributes in controllers.
app_home:
path: /
controller: App\Controller\HomeController::index
Key Entry Points:
AppKernel (Symfony 5+) or use Kernel class in config/bundles.php.php bin/console cache:clear
Dependency Injection (DI) Integration:
config/services.yaml.
services:
App\Service\MyService:
arguments:
$param: '%env(APP_PARAM)%'
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MyCompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
$container->getDefinition('my_service')->setArgument(0, 'custom_value');
}
}
Register in src/Kernel.php:
protected function build(ContainerBuilder $container): void {
$container->addCompilerPass(new MyCompilerPass());
}
Configuration Management:
%env() placeholders in framework.yaml:
framework:
secret: '%env(APP_SECRET)%'
ParameterBagInterface:
$this->container->getParameter('app.some_param');
Routing and Controllers:
#[Route('/profile', name: 'app_profile')]
public function profile(): Response { ... }
config/packages/framework.yaml:
framework:
router:
annotation_directory: '%kernel.project_dir%/src/Controller'
Testing Patterns:
KernelTestCase for full-stack testing:
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MyTest extends KernelTestCase {
public function testSomething() {
$client = static::createClient();
$client->request('GET', '/');
$this->assertResponseIsSuccessful();
}
}
$this->container->set('my_service', $mockService);
Event Listeners/Subscribers:
config/services.yaml:
services:
App\EventListener\MyListener:
tags:
- { name: 'kernel.event_listener', event: 'kernel.request', method: 'onKernelRequest' }
Bundles:
config/bundles.php:
return [
// ...
Symfony\WebServerBundle\WsserverBundle::class => ['all' => true],
];
HTTP Cache:
framework.yaml:
framework:
http_cache:
enabled: true
lifetime: 3600
Error Handling:
config/packages/twig.yaml and templates/bundles/framework/exceptions.html.twig.Debugging:
.env:
APP_ENV=dev
APP_DEBUG=1
/_profiler.Caching Issues:
config/ or src/ not reflected.php bin/console cache:clear
cache:pool:prune to clean stale cache files.Router Context:
router.request_context.base_url not updating with default_uri.default_uri is set in framework.yaml and the cache is cleared.Session Problems:
MockSessionStorage or ensure cookie_lifetime is set in framework.yaml:
framework:
session:
cookie_lifetime: 3600
storage_factory_id: session.storage.mock_file
KernelTestCase Quirks:
ensureKernelShutdown():
protected function ensureKernelShutdown(): void {
parent::ensureKernelShutdown();
$this->container->get('kernel')->terminate($this->container, new Request());
}
Attribute Routing:
annotation_reader is enabled in framework.yaml for backward compatibility:
framework:
router:
annotation_reader: 'annotation_reader'
Service Mocking:
MockBuilder for decorated services:
$mock = $this->createMock(DecoratedService::class);
$this->container->set('decorated_service', $mock);
Config Reference:
debug:config fails with TypeError on scalars.ConfigDebugCommand.Debug Configuration:
php bin/console debug:config framework
Event Dispatcher:
php bin/console debug:event-dispatcher
Container Dumping:
php bin/console debug:container --parameters
Environment Variables:
.env files:
php bin/console debug:env
Custom Compiler Passes:
Event Subscribers:
kernel.request, kernel.response).HttpKernel Interface:
HttpKernelInterface for advanced request/response handling.Routing Resolvers:
UrlGenerator or RouterInterface implementations.Configuration Normalizers:
ConfiguratorInterface for custom config validation/transformation.ParamConfigurator: For dynamic parameter values in config:
framework:
secret: '%param("app.secret")%'
KernelTestCase: For isolated, fast tests with a fresh container.cache:pool:prune to clean up stale cache files during development.APP_DEBUG=1 and use /_profiler to inspect requests, events, and services.How can I help you explore Laravel packages today?