2012-04-21 7 views
7

私は、ファイルを置き換える必要がありSymfony2:コアテンプレートをオーバーライドする方法は?

\app\Resources\SensioGeneratorBundle\skeleton\crud\views\index.html.twig 

を作成することにより、SymfonyGeneratorBundleテンプレートを上書きしようとしています:

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

をしかし、それはまだW/Oなどの新しいバンドルを作成することさえ行う方法cache:clear.後に元のファイルを使用していますCan't override the standard skeleton views in Symfony2 GeneratorBundle

+0

は、それが代わりに「index.html.twig.twig」拡張子ではないでしょうか?ダブル「小枝」に注意してください。 http://symfony.com/doc/current/bundles/SensioGeneratorBundle/index.html – DevWL

答えて

14

// 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); 
} 

あなたは YourBundle\Resourceskeletonフォルダをコピーして、テンプレートを変更する必要があり、右 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(); 

SensioGeneratorBundle後に続いてYourBundle.phpオーバーライドregisterCommands方法であなたのバンドルを登録します。

+0

私は正確にこのエラーメッセージを受け取りました: [Twig_Error_Loader] "C:\ wamp \ apps \ rerent \ src \ Rent \ ProgramBundle /../ Resources/skeleton/crud "ディレクトリが存在しません。 私はそれがアプリ/フォルダからではなく、バンドルから読み取ろうと思います。助けて? – Zeljko

+0

このアプローチの注意点を忘れてしまった場合は、スケルトンフォルダをProgramBundleにコピーする必要があります。また、 '../'は必須ではありません。編集されたコード。 –

+0

それは働いたか。 –

0

m2mdasの答えは私のために働いたが、それだけではなく、ファイルシステムの

ファイルシステムを読み込む必要があることを発見した後に!

これを確認するためにあなたのベンダー/ symfonyの/.../ファイルシステムフォルダを見てみましょう。

11

ファイルコピーし、2.3以降のバージョンでは、例えば、編集テンプレートを上書きするには、次のディレクトリの

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

:単にデフォルトコマンドでCRUDを生成し、今

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

をし、新しいテンプレートが使用されます。

関連する問題