Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override the standard skeleton views in Symfony2 GeneratorBundle

I don't manage to override the skeleton views of the generatorBundle.

I've first tried by adding my view in /app/Resources/SensioGeneratorBundle/skeleton/crud/views/index.html.twig

It didn't worked so I tried to create a new Bundle extending SensioGeneratorBundle and copy the my view in its Resources folder.

I already manage to use themes for twig forms, but I need to personalize the views generated by the doctrine:generate:crud command.

like image 651
Julien Ducro Avatar asked Aug 29 '11 07:08

Julien Ducro


2 Answers

First of all: The corresponding skeleton views are located here:

vendor/bundles/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud

Quick and dirty you should be fine by overriding these view files - but thats not what we want ;)

In:

vendor/bundles/Sensio/Bundle/GeneratorBundle/Command/GenerateDoctrineCrudCommand.php

there is an accessor for the Generator:

protected function getGenerator()
{
    if (null === $this->generator) {
        $this->generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
    }

    return $this->generator;
} 

One can try to override this method in your extending Bundle and set a different $skeletonDir in the constructor.

Edit:

Quick example in my test environment how it can be achieved (I only made a quick test ;):

Generate a new bundle for the custom generator: php app/console generate:bundle and follow the instructions. A route is not needed. I chose for this example: Acme/CrudGeneratorBundle (Or use an existing bundle)

Create a folder called "Command" in the newly created bundle directory.

Place a command class in this folder.

<?php
//src/Acme/CrudGeneratorBundle/Command/MyDoctrineCrudCommand.php

namespace Acme\CrudGeneratorBundle\Command;

use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;

class MyDoctrineCrudCommand extends \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
{
    protected function configure()
    {
        parent::configure();
        $this->setName('mydoctrine:generate:crud');
    }

    protected function getGenerator()
    {
        $generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
        $this->setGenerator($generator);
        return parent::getGenerator();
    }
}

Copy the vendor/bundles/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud to your Resources (in my example "src/Acme/CrudGeneratorBundle/Resources/crud")

like image 143
madflow Avatar answered Oct 17 '22 06:10

madflow


This was the best solution for me: symfony2-how-to-override-core-template

doesn't add a command but modifies the skeleton for that particular bundle.

like image 21
mameluc Avatar answered Oct 17 '22 06:10

mameluc