Symfony2:如何覆盖核心模板?

时间:2012-04-21 17:29:08

标签: php symfony

我试图通过创建

来覆盖SymfonyGeneratorBundle模板
\app\Resources\SensioGeneratorBundle\skeleton\crud\views\index.html.twig

该文件应该替换:

\vendor\bundles\Sensio\Bundle\GeneratorBundle\Resources\skeleton\crud\views\index.html.twig

但即使在cache:clear.之后它仍然使用原始文件如何做到这一点而不创建像Can't override the standard skeleton views in Symfony2 GeneratorBundle这样的新包?

3 个答案:

答案 0 :(得分:14)

SensioGeneratorBundle app/AppKernel.php之后立即注册您的捆绑包:

// app/AppKernel.php

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
    //.....
    $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    $bundles[] = new Namespace\YourBundle();
}

// Or outside if, should you want your bundle to be available in production environment
$bundles[] = new Namespace\YourBundle();

然后在YourBundle.php覆盖registerCommands方法,

// Bundle\YourBundle.php

// other declarations
use Symfony\Component\Console\Application;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
use Symfony\Component\Filesystem\Filesystem;


public function registerCommands(Application $application){
    $crudCommand = $application->get('generate:doctrine:crud');
    $generator = new DoctrineCrudGenerator(new FileSystem, __DIR__.'/Resources/skeleton/crud');
    $crudCommand->setGenerator($generator);

    parent::registerCommands($application);
}

您必须将skeleton文件夹复制到YourBundle\Resource并修改模板。

答案 1 :(得分:11)

要覆盖编辑模板,例如,在2.3+版本中,请复制文件:

vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud/views/edit.html.twig.twig

目录:

app/Resources/SensioGeneratorBundle/skeleton/crud/views/edit.html.twig.twig

现在,只需使用默认命令生成crud,它将使用新模板。

答案 2 :(得分:0)

m2mdas的答案对我有用,但只有在发现它应该阅读之后

文件系统而不是FileSystem!

查看您的供应商/ symfony /.../ Filesystem文件夹以验证这一点。