symfony/flex
Symfony Flex is a Composer plugin that streamlines installing and configuring Symfony packages. It uses recipes to auto-enable bundles, add config, env vars, and scripts, and keeps projects consistent across environments with minimal manual setup.
Installation: Add Symfony Flex to your Laravel project via Composer:
composer require symfony/flex
This installs the plugin globally for your project, enabling recipe-based dependency management.
First Use Case:
Install a Symfony component (e.g., symfony/mailer) with auto-generated configs:
composer require symfony/mailer
Flex automatically:
composer.json..env variables (e.g., MAILER_DSN).config/packages/mailer.yaml if using YAML configs).Where to Look First:
symfony.lock file tracks installed recipes and their versions.composer recipes:list to see installed recipes and composer recipes:update to update them.Dependency Management:
composer require for Symfony packages (e.g., symfony/console, api-platform/core). Flex handles the rest.composer require symfony/console --dev
This installs the package and generates a config/packages/console.yaml file.Updating Dependencies:
composer recipes:update
--no-changelog to skip changelog generation:
composer recipes:update --no-changelog
Environment Configuration:
.env variables for Symfony packages. Override them in your .env file as needed.symfony/mailer adds MAILER_DSN, customize it in .env:
MAILER_DSN=smtp://user:pass@smtp.example.com:2525
Laravel-Specific Patterns:
symfony/http-client) while keeping Laravel’s core workflows intact.config/ with Symfony’s config/packages/ by:
config/packages/ (Flex handles this).HttpClient in Laravel’s AppServiceProvider).CI/CD Integration:
composer.json scripts for automated updates:
"scripts": {
"post-install-cmd": [
"@symfony/flex:composer:install"
],
"post-update-cmd": [
"@symfony/flex:composer:update"
]
}
composer recipes:update in CI to ensure consistent environments.Custom Recipes:
recipes/YourVendor/YourPackage/.Laravel Service Providers:
AppServiceProvider:
use Symfony\Component\HttpClient\HttpClient;
public function register()
{
$this->app->singleton('symfony.http_client', function () {
return HttpClient::create();
});
}
Environment Variables:
.env variables with SYMFONY_ to avoid conflicts with Laravel’s variables (e.g., SYMFONY_MAILER_DSN).Docker and Flex:
DockerComposeConfigurator to manage Docker-specific configs. Example:
# docker-compose.yml
services:
app:
image: your-app
volumes:
- .:/var/www/html
- ./config/packages:/var/www/html/config/packages # Symfony configs
Debugging Configs:
php bin/console debug:container --parameters
Symfony Runtime:
symfony/runtime with Flex for optimized bootstrapping:
composer require symfony/runtime
File Overwrites:
--yes to skip confirmation:
composer recipes:install --yes
.gitignore or .symfony.lock exclusions.Environment Conflicts:
.env keys. Prefix Symfony keys (e.g., SYMFONY_) to avoid conflicts.Recipe Conflicts:
composer recipes:remove for problematic packages.Composer Scripts:
composer dump-autoload manually after Flex updates, as Flex handles this automatically.Symfony Lock File:
symfony.lock file tracks recipe versions. Commit it to version control to ensure consistency across environments.Private Recipes:
composer require your/private-package --auth=github-oauth
Verbose Output:
-v for debugging:
composer recipes:update -v
Check Installed Recipes:
composer recipes:list
Update Specific Recipes:
composer recipes:update YourVendor/YourPackage
Symfony Debug Commands:
php bin/console debug:config mailer
Clear Cache:
php bin/console cache:clear
Custom Configurators:
ComposerScriptsConfigurator) to handle project-specific logic. Example:
// In a custom recipe
public function configure(Configuration $configuration)
{
$configuration->setConfigurator(new class implements ConfiguratorInterface {
public function configure(Configuration $configuration)
{
// Custom logic here
}
});
}
Recipe Development:
recipes/
YourVendor/
YourPackage/
YourPackageRecipe.php
resources/
config/
packages/
your_package.yaml
Environment-Specific Recipes:
# composer.json
"extra": {
"symfony": {
"require": {
"dev": ["symfony/debug-bundle"]
}
}
}
Post-Install Scripts:
composer.json to run after Flex updates:
"scripts": {
"post-recipes-update": [
"@php artisan config:clear"
]
}
Symfony Runtime Integration:
symfony/runtime with Flex to optimize bootstrapping:
composer require symfony/runtime
config/bundles.php:
return [
// ...
Symfony\UX\RuntimeBundle\RuntimeBundle::class => ['all' => true],
];
Leverage Symfony Packs:
symfony/webpack-encore) with Flex for seamless integration:
composer require symfony/webpack-encore
Hybrid Testing:
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyTest extends WebTestCase {
// Test Symfony services in Laravel
}
Flex with Laravel Mix:
webpack-encore with Laravel Mix for asset management:
// webpack.mix.js
const { mix } = require('laravel-mix');
const Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('public/build')
How can I help you explore Laravel packages today?