Installation
Add the bundle to your composer.json:
composer require 4h/generatorbundle
Register it in config/bundles.php:
return [
// ...
FourH\GeneratorBundle\FourHGeneratorBundle::class => ['all' => true],
];
First Command Generate a basic CRUD controller:
php bin/console generate:controller --entity=App\Entity\Post --no-interaction
(Check bin/console for available commands—this bundle extends Symfony’s sensio:generator with custom templates.)
Key Files to Review
config/packages/four_h_generator.yaml (default config, if present)templates/ (custom generator templates, if extended)src/Generator/ (core logic, if overriding behavior).Custom Templates
Override default templates by copying them from vendor/4h/generatorbundle/templates/ to templates/four_h_generator/ in your project. Example:
mkdir -p templates/four_h_generator
cp vendor/4h/generatorbundle/templates/controller/* templates/four_h_generator/
Update config/packages/four_h_generator.yaml to point to your custom path:
four_h_generator:
templates_dir: '%kernel.project_dir%/templates/four_h_generator'
Dynamic Generation
Use --dry-run to preview changes before applying:
php bin/console generate:controller --entity=App\Entity\User --dry-run
Chain commands with --no-interaction for CI/CD pipelines.
Entity-Specific Logic Extend generators by creating a custom command:
// src/Command/CustomGeneratorCommand.php
namespace App\Command;
use FourH\GeneratorBundle\Generator\Generator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomGeneratorCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$generator = $this->getContainer()->get(Generator::class);
$generator->generate('custom_template', ['entity' => 'App\Entity\Post']);
}
}
Integration with Doctrine Generate repositories, services, or DTOs alongside entities:
php bin/console generate:repository --entity=App\Entity\Product
php bin/console generate:dto --entity=App\Entity\Product
Template Overrides
php bin/console debug:generator to list available templates and their paths.Namespace Conflicts
src/Controller/ already has PostController).--target-dir to specify output:
php bin/console generate:controller --entity=App\Entity\Post --target-dir=src/Generated/
Doctrine Metadata Cache
php bin/console cache:clear
php bin/console doctrine:cache:clear-metadata
Bundle Updates
composer.json’s "replace" or "extra" fields to pin versions or document overrides.-vvv to commands for detailed logs:
php bin/console generate:controller -vvv --entity=App\Entity\Post
--dry-run before committing changes.generator.generate):
# config/services.yaml
services:
App\EventListener\CustomGeneratorListener:
tags:
- { name: kernel.event_listener, event: generator.generate, method: onGenerate }
Custom Generators
Create new generator types by extending FourH\GeneratorBundle\Generator\Generator and register them in services.yaml:
services:
app.custom_generator:
class: App\Generator\CustomGenerator
tags: [four_h_generator.generator]
Pre/Post-Generation Hooks
Use Symfony’s kernel.request event to validate entities before generation or clean up afterward.
Template Variables
Pass custom data to templates via --option flags or environment variables:
php bin/console generate:controller --entity=App\Entity\Post --namespace=Admin
How can I help you explore Laravel packages today?