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.
## Getting Started
### Minimal Steps
1. **Installation**:
```bash
composer require symfony/framework-bundle:^8.1.0-BETA3
Note: This is a beta release (v8.1.0-BETA3). Ensure compatibility with Symfony 8.1+ and test thoroughly in staging.
First Use Case:
App\Kernel (extends Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait or AbstractKernel).
ContainerInterface must be explicitly typed).config/packages/framework.yaml (e.g., secret, http_client, router).
framework.http_client.throw config to control exception handling for HTTP client failures (default: false).#[Route]) or YAML/XML files. The bundle auto-registers routes via Kernel::getProjectDir().
Where to Look First:
config/packages/framework.yaml: Core framework settings (new: http_client.throw).src/Kernel.php: Kernel class (now requires explicit MicroKernelTrait extension; check for new constructor type hints).config/routes/: Routing definitions (prefer attributes for Symfony 8.1+).tests/: Use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase (ensure bootKernel() is called in setUp() for beta stability).Dependency Injection (DI) and Services:
#[Autowire] or tagged are auto-discovered. For Symfony 8.1+, ensure:
# config/services.yaml
parameters:
kernel.secret: '%env(APP_SECRET)%'
framework:
autowire: true
auto_mapping: true
container.dumper.inline_class_loader: true # New: Improves dev performance
Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface for container modifications.
php bin/console debug:container or php bin/console cache:dump to rebuild the container.
--format=json flag to debug:container for machine-readable output.Routing:
#[Route] over annotations (Symfony 8.1+):
#[Route('/profile', name: 'app_profile', methods: ['GET', 'HEAD'])]
public function profile(): Response { ... }
methods attribute was ignored in some edge cases.# config/routes.yaml
app_homepage:
path: /{page}
controller: App\Controller\PageController::index
requirements:
page: \d+
defaults:
_locale: '%locale%' # New: Supports parameterized defaults
framework:
router:
cache_warmup: true
route_cache_warmup: true # New: Separate config for route cache
HTTP Request/Response Handling:
Symfony\Component\HttpFoundation\Request:
public function index(Request $request): Response {
$query = $request->query->get('q');
$this->denyAccessUnlessGranted('ROLE_USER'); # New: Shortcut for security checks
}
Symfony\Component\HttpFoundation\Response or shortcuts:
return $this->json(['data' => 'value'], 200, [], ['Cache-Control' => 'public']);
return new RedirectResponse('/login', 302);
Response::createFromContent() factory method for easier response creation.Configuration Management:
%env() in framework.yaml:
framework:
secret: '%env(APP_SECRET)%'
http_client:
timeout: '%env(int:HTTP_CLIENT_TIMEOUT)%' # New: Type-casting support
$this->getParameter('param_name') or $container->getParameter('param_name').
.env (APP_DEBUG=1) or override in Kernel::isDebug().
error_log by default.Testing:
KernelTestCase:
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MyTest extends KernelTestCase {
protected function setUp(): void {
parent::setUp();
self::bootKernel(); // Critical for beta stability
}
public function testHomepage() {
$client = static::createClient();
$client->request('GET', '/');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Welcome'); # New: Assertion helper
}
}
self::$container->get('service')->method('...')->willReturn(...).
createMockBuilder() helper to KernelTestCase for easier mocking.$client->getContainer()->get('kernel')->warmup() before tests.
Event Listeners/Subscribers:
KernelEvents::REQUEST, KernelEvents::TERMINATE, etc.:
#[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest', priority: 10)]
public function onKernelRequest(RequestEvent $event): void {
if (!$event->isMainRequest()) {
return;
}
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
}
kernel.event_listener:
#[Tag('kernel.event_listener')]
class MyListener { ... }
Console Integration:
src/Command/ and tag them:
#[AsCommand(name: 'app:greet', description: 'Greets a user')]
class GreetCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output): int {
$output->writeln(sprintf('Hello %s!', $input->getArgument('name')));
return Command::SUCCESS;
}
}
#[AsCommand] attribute support for Symfony 8.1+.debug:container, debug:config, or debug:router.
debug:config now correctly shows merged configuration for all environments.Caching:
framework.yaml:
framework:
http_cache:
enabled: true
life_time: 3600
etag: true # New: Enable ETag support
php bin/console cache:pool:prune to clean stale cache.
--dry-run flag to preview pruning results.Bundles:
config/bundles.php. Example:
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
App\Kernel::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
];
Environment-Specific Config:
config/packages/{env}/framework.yaml (e.g., framework.prod.yaml).framework.{env}.{suffix}.yaml (e.g., framework.prod.db.yaml).Debugging:
APP_DEBUG=1) and profiler (APP_ENV=dev).#[DumpServer] to inspect variables during requests.
How can I help you explore Laravel packages today?