Can't override the standard skeleton views in Symfony2 GeneratorBundle

我们两清 提交于 2020-01-14 08:53:24

问题


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.


回答1:


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")




回答2:


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.



来源:https://stackoverflow.com/questions/7227082/cant-override-the-standard-skeleton-views-in-symfony2-generatorbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!