Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Maker Bundle Laravel Package

symfony/maker-bundle

Symfony MakerBundle speeds up development by generating boilerplate code via simple CLI commands. Create controllers, entities, forms, tests, and more with best-practice templates, consistent structure, and quick scaffolding for common Symfony tasks.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require symfony/maker-bundle --dev
    

    Register the bundle in config/bundles.php (auto-discovered in Symfony 4.3+).

  2. First Command Generate a basic controller:

    php bin/console make:controller
    

    Follow prompts (e.g., Blog/PostController, --format=annotation).

  3. Key Files to Review

    • config/packages/maker.yaml (customization options).
    • src/Maker/ (default maker classes, e.g., ControllerMaker.php).

First Use Case: Scaffold a CRUD Entity

php bin/console make:crud Post blog/post
  • Generates:
    • Entity (Post with timestamps, slug, etc.).
    • Controller (with index, show, new, edit, update, delete).
    • Form type (PostType).
    • Twig templates (index.html.twig, edit.html.twig, etc.).
    • Repository interface (PostRepository).
  • Pro Tip: Use --no-src to skip source files (e.g., for API-only projects).

Implementation Patterns

Core Workflows

  1. Entity Generation with Relationships

    php bin/console make:entity Post
    
    • Add fields interactively (e.g., title:string, content:text).
    • Define relationships (e.g., ManyToOne:User).
    • Output: Migrations, entity class, and repository.
  2. 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
    
  3. CRUD with API Support Use --api flag for API-specific CRUD:

    php bin/console make:crud Post blog/post --api
    
    • Generates:
      • Serializer groups (@Groups({"post:read", "post:write"})).
      • API platform resources (if installed).
      • JSON responses.
  4. Form Types Generate a form for an existing entity:

    php bin/console make:form PostType Post
    
    • Customize fields with --fields (e.g., --fields="title:text(255)").
  5. Event Listeners/Subscribers

    php bin/console make:event-listener
    
    • Choose event (e.g., Kernel::TERMINATE).
    • Define logic in the generated class.

Integration Tips

  • Doctrine Fixtures: Combine with doctrine/doctrine-fixtures-bundle to auto-generate fixtures for new entities:
    php bin/console make:fixtures
    
  • API Platform: Use --api with api-platform/core for instant API endpoints.
  • Symfony UX: Pair with symfony/ux for Turbo/Stimulus-ready templates:
    php bin/console make:crud Post blog/post --format=turbo
    
  • Testing: Generate test classes alongside entities/controllers:
    php bin/console make:test
    

Gotchas and Tips

Pitfalls

  1. Namespace Conflicts

    • If your bundle/class names conflict with Symfony’s, override the getTemplate() method in custom makers to point to your own templates (e.g., templates/custom/controller.twig).
  2. Overwriting Existing Files

    • Makers default to --force (overwrite). Use --dry-run to preview changes:
      php bin/console make:crud Post blog/post --dry-run
      
    • To skip existing files, use --no-backup (but back up manually first).
  3. Doctrine Migrations

    • Entity makers generate migrations, but they may conflict with existing ones. Run:
      php bin/console make:migration
      
      after generation to update the migration table.
  4. Twig Template Paths

    • Custom templates must be placed in templates/maker/ (relative to your bundle) or configured in maker.yaml:
      templates:
          maker: '%kernel.project_dir%/custom_templates'
      
  5. PHP 8+ Attributes

    • If using PHP 8+, ensure your composer.json has "platform": { "php": "8.1" } to avoid attribute parsing issues.

Debugging

  1. Dry Runs Always use --dry-run to inspect generated code before applying:

    php bin/console make:controller --dry-run
    
  2. Log Output Enable debug mode to see maker execution details:

    php bin/console debug:maker
    
  3. 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(...);
    }
    

Tips

  1. Aliases for Speed Create shell aliases in ~/.bashrc or ~/.zshrc:

    alias makec="php bin/console make:crud"
    alias makee="php bin/console make:entity"
    
  2. Template Customization Override default Twig templates (e.g., controller.twig) in templates/maker/ to modify generated code globally.

  3. Environment-Specific Generation Use --env=test to generate test-specific files (e.g., controllers with @IsGranted("ROLE_TEST")).

  4. IDE Integration

    • PHPStorm: Mark src/Maker/ as a "Generated Source Root" to avoid IDE warnings.
    • VSCode: Add to settings.json:
      "files.exclude": {
          "**/src/Maker/**": false
      }
      
  5. 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 }
    
  6. Performance

    • Cache generated files in var/cache/dev/maker (cleared on cache:clear).
    • For large projects, regenerate makers occasionally to avoid template drift.
  7. Team Consistency

    • Commit maker-generated files to version control (e.g., .gitignore exceptions for src/Entity/, src/Controller/).
    • Document custom makers in CONTRIBUTING.md for onboarding.
  8. Legacy Code

    • Use --with-doctrine-orm=false for non-Doctrine projects (e.g., Eloquent).
    • For Symfony 3.x, pin to ^1.0 in composer.json (Symfony Maker Bundle requires Symfony 4.3+).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope