Php Symfony2:如何覆盖核心模板?

Php Symfony2:如何覆盖核心模板?,php,symfony,Php,Symfony,我试图通过创建 \app\Resources\SensioGeneratorBundle\skeleton\crud\views\index.html.twig 该文件应替换为: \vendor\bundles\Sensio\Bundle\GeneratorBundle\Resources\skeleton\crud\views\index.html.twig 但即使在cache:clear之后,它仍然使用原始文件。如何在创建新包的情况下这样做?在app/AppKernel.php中的Sen

我试图通过创建

\app\Resources\SensioGeneratorBundle\skeleton\crud\views\index.html.twig
该文件应替换为:

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

但即使在
cache:clear之后,它仍然使用原始文件。
如何在创建新包的情况下这样做?

app/AppKernel.php
中的
SensioGeneratorBundle
之后立即注册您的包,例如:

// 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
override
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
并修改模板。

m2mdas的答案对我有效,但只有在发现它应该是

文件系统而不是文件系统


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

要覆盖编辑模板,例如,在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,它将使用新模板。

我确实这样做了,并得到了以下错误消息:[Twig\u error\u Loader]C:\wamp\apps\rerent\src\Rent\ProgramBundle/./Resources/skeleton/crud”目录不存在。我猜它试图从捆绑包中读取,而不是从应用程序/文件夹中读取。帮助?忘了告诉您这种方法的一个警告,您必须将骨架文件夹复制到您的程序包中。也不需要“../”。编辑代码。是。我将骨架复制到Bundle\MyBundle\Resources,它工作得非常好。非常感谢。请注意,这将在最新版本的捆绑包中发生变化:您可以通过将模板放置在app/Resources/SensioGeneratorBundle/skeletonI中来覆盖模板。您感觉自己想编写
新文件系统(\uu DIR\uuu.'./Resources/skeleton/crud')
而不是
新文件系统,\uu DIR\uu….
它不应该是一个新文件系统吗改为“index.html.twig.twig”扩展名?请注意双“twig”。