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

Generator Bundle Laravel Package

sensio/generator-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. Installation (Legacy Context) Since this bundle is archived and incompatible with Symfony 4+, use it only for legacy Symfony 2/3 projects. Install via Composer:

    composer require sensio/generator-bundle
    
  2. Enable the Bundle Add to AppKernel.php (Symfony 2) or bundles.php (Symfony 3):

    new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(),
    
  3. First Use Case: Generate a CRUD Controller Run the interactive command:

    php app/console generate:doctrine:crud
    

    Follow prompts to specify:

    • Entity class (e.g., AcmeBlogBundle:Post)
    • Format (e.g., yml, xml, php)
    • Output directory (default: src/Acme/BlogBundle/Controller/)
  4. Key Documentation

    • Official Symfony Docs
    • Focus on the generate: namespace commands (e.g., generate:doctrine:form, generate:bundle).

Implementation Patterns

Core Workflows

  1. Doctrine-Driven Generation

    • Entities to CRUD: Use generate:doctrine:crud to scaffold controllers, forms, and templates from an existing Doctrine entity.
      php app/console generate:doctrine:crud AcmeBlogBundle:Post
      
    • Forms: Generate form classes with generate:doctrine:form for specific fields.
      php app/console generate:doctrine:form AcmeBlogBundle:Post title content
      
  2. Bundle Scaffolding

    • Create a new bundle skeleton with metadata:
      php app/console generate:bundle --namespace=Acme/BlogBundle --format=yml --dir=src --bundle-name=BlogBundle
      
    • Customize via --format (yml/xml/php) and --dir (output path).
  3. Interactive Prompts

    • All commands are interactive. Use --no-interaction for scripting:
      php app/console generate:doctrine:crud AcmeBlogBundle:Post --no-interaction
      
  4. Template Customization

    • Override default templates by copying files from vendor/sensio/generator-bundle/Resources/SensioGeneratorBundle to app/Resources/SensioGeneratorBundle.

Integration Tips

  • Post-Generation Hooks: Extend the bundle by overriding generator classes (e.g., Sensio\Bundle\GeneratorBundle\Generator\Doctrine\CrudGenerator). Place custom logic in app/Resources/SensioGeneratorBundle/Generator/.
  • Symfony 3 Compatibility: Use --symfony-dir to specify the Symfony root (e.g., --symfony-dir=../symfony for monorepos).
  • Doctrine Metadata: Ensure your entity metadata (annotations/YAML/XML) is up-to-date before generation.

Gotchas and Tips

Pitfalls

  1. Symfony 4+ Incompatibility

    • Error: Class 'Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle' not found in Symfony 4+.
    • Fix: Use MakerBundle instead. This bundle is not maintained for modern Symfony.
  2. Entity Not Found

    • Error: No such entity or mapping during CRUD generation.
    • Debug: Verify the entity exists in Doctrine metadata (check php app/console doctrine:schema:validate).
  3. Template Overrides Not Loading

    • Issue: Custom templates in app/Resources/SensioGeneratorBundle are ignored.
    • Fix: Ensure the directory structure mirrors the original (e.g., Resources/SensioGeneratorBundle/Generator/Doctrine/templates/crud.twig).
  4. Namespace Conflicts

    • Error: Generated code uses incorrect namespaces (e.g., use Acme\BlogBundle; instead of use Acme\BlogBundle\Entity;).
    • Fix: Manually adjust the --namespace flag or override templates to hardcode paths.

Debugging Tips

  • Dry Run: Use --dry-run to preview changes without writing files:
    php app/console generate:doctrine:crud AcmeBlogBundle:Post --dry-run
    
  • Verbose Output: Add -v for detailed logs:
    php app/console generate:bundle -v
    
  • Check Generated Files: Verify output in src/ or custom directories. Common artifacts:
    • Controller classes (e.g., PostController.php)
    • Form types (e.g., PostType.php)
    • Twig templates (e.g., Resources/views/Post/index.html.twig).

Extension Points

  1. Custom Generators Extend Sensio\Bundle\GeneratorBundle\Generator\Generator to create reusable generators. Example:

    // app/Resources/SensioGeneratorBundle/Generator/MyCustomGenerator.php
    namespace AppBundle\Generator;
    use Sensio\Bundle\GeneratorBundle\Generator\Generator;
    class MyCustomGenerator extends Generator {
        public function generate() { ... }
    }
    

    Register in services.yml:

    services:
        app.my_custom_generator:
            class: AppBundle\Generator\MyCustomGenerator
            tags:
                - { name: sensio_generator.generator }
    
  2. Command Overrides Override existing commands by extending Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand and updating the service definition.

  3. Configuration Customize behavior via config.yml:

    sensio_generator:
        bundle_name_generator: '%app.bundle_name_generator.class%' # Custom bundle name logic
        bundle_prefix_generator: '%app.bundle_prefix_generator.class%' # Custom prefix logic
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware