symfony/maker-bundle
Symfony Maker Bundle accelerates Symfony development by generating boilerplate code via maker commands. Quickly create controllers, entities, forms, tests, security, and more, following best practices, so you can focus on building features instead of scaffolding.
Installation
composer require symfony/maker-bundle --dev
Register the bundle in config/bundles.php (auto-discovered in Symfony 4.3+).
First Command Generate a basic controller:
php bin/console make:controller
Follow prompts (e.g., Blog/PostController, --format=annotation).
Key Files to Review
config/packages/maker.yaml (customization options).src/Maker/ (default maker classes, e.g., ControllerMaker.php).php bin/console make:crud Post blog/post
Post with timestamps, slug, etc.).index, show, new, edit, update, delete).PostType).index.html.twig, edit.html.twig, etc.).PostRepository).--no-src to skip source files (e.g., for API-only projects).Entity Generation with Relationships
php bin/console make:entity Post
title:string, content:text).ManyToOne:User).Customizing Makers
Extend Maker\Maker to create reusable templates:
// src/Maker/CustomControllerMaker.php
namespace App\Maker;
use Symfony\Bundle\MakerBundle\Maker\Generator;
use Symfony\Bundle\MakerBundle\Maker\MakerInterface;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
class CustomControllerMaker implements MakerInterface
{
public function configureInput(InputConfiguration $input): void
{
$input->setOption('format', 'annotation');
}
public function generate(InputConfiguration $input, Generator $generator): void
{
$name = Str::asClassName($input->getArgument('name'));
$generator->generateClass(...);
}
}
Register in config/packages/maker.yaml:
makers:
custom_controller:
command: make:custom-controller
class: App\Maker\CustomControllerMaker
CRUD with API Support
Use --api flag for API-specific CRUD:
php bin/console make:crud Post blog/post --api
@Groups({"post:read", "post:write"})).Form Types Generate a form for an existing entity:
php bin/console make:form PostType Post
--fields (e.g., --fields="title:text(255)").Event Listeners/Subscribers
php bin/console make:event-listener
Kernel::TERMINATE).doctrine/doctrine-fixtures-bundle to auto-generate fixtures for new entities:
php bin/console make:fixtures
--api with api-platform/core for instant API endpoints.symfony/ux for Turbo/Stimulus-ready templates:
php bin/console make:crud Post blog/post --format=turbo
php bin/console make:test
Namespace Conflicts
getTemplate() method in custom makers to point to your own templates (e.g., templates/custom/controller.twig).Overwriting Existing Files
--force (overwrite). Use --dry-run to preview changes:
php bin/console make:crud Post blog/post --dry-run
--no-backup (but back up manually first).Doctrine Migrations
php bin/console make:migration
after generation to update the migration table.Twig Template Paths
templates/maker/ (relative to your bundle) or configured in maker.yaml:
templates:
maker: '%kernel.project_dir%/custom_templates'
PHP 8+ Attributes
composer.json has "platform": { "php": "8.1" } to avoid attribute parsing issues.Dry Runs
Always use --dry-run to inspect generated code before applying:
php bin/console make:controller --dry-run
Log Output Enable debug mode to see maker execution details:
php bin/console debug:maker
Custom Maker Debugging
Add dump() calls in your custom maker’s generate() method to inspect variables:
public function generate(InputConfiguration $input, Generator $generator): void
{
dump($input->getArgument('name')); // Debug input
$generator->generateClass(...);
}
Aliases for Speed
Create shell aliases in ~/.bashrc or ~/.zshrc:
alias makec="php bin/console make:crud"
alias makee="php bin/console make:entity"
Template Customization
Override default Twig templates (e.g., controller.twig) in templates/maker/ to modify generated code globally.
Environment-Specific Generation
Use --env=test to generate test-specific files (e.g., controllers with @IsGranted("ROLE_TEST")).
IDE Integration
src/Maker/ as a "Generated Source Root" to avoid IDE warnings.settings.json:
"files.exclude": {
"**/src/Maker/**": false
}
Post-Generation Hooks
Use Symfony’s kernel.terminate event to run scripts after generation (e.g., auto-commit changes):
// config/services.yaml
App\EventListener\PostMakeListener:
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: onTerminate }
Performance
var/cache/dev/maker (cleared on cache:clear).Team Consistency
.gitignore exceptions for src/Entity/, src/Controller/).CONTRIBUTING.md for onboarding.Legacy Code
--with-doctrine-orm=false for non-Doctrine projects (e.g., Eloquent).^1.0 in composer.json (Symfony Maker Bundle requires Symfony 4.3+).How can I help you explore Laravel packages today?