2012-04-21 38 views
7

Tôi cố gắng để ghi đè SymfonyGeneratorBundle mẫu bằng cách tạoSymfony2: cách ghi đè lên mẫu lõi?

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

tập tin đó sẽ thay thế:

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

Nhưng nó vẫn sử dụng tập tin gốc ngay cả sau khi cache:clear. Làm thế nào để làm điều đó w/o tạo bó mới như Can't override the standard skeleton views in Symfony2 GeneratorBundle?

+0

không phải là tiện ích mở rộng "index.html.twig.twig" thay thế? Lưu ý đôi "cành". http://symfony.com/doc/current/bundles/SensioGeneratorBundle/index.html – DevWL

Trả lời

14

đăng ký gói của bạn ngay sau khi SensioGeneratorBundle trong app/AppKernel.php ví dụ .:

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

Sau đó, trong YourBundle.php override registerCommands phương pháp,

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

Bạn phải sao chép skeleton thư mục để YourBundle\Resource và sửa đổi mẫu.

+0

Tôi đã làm chính xác và nhận được thông báo lỗi này: [Twig_Error_Loader] "C: \ wamp \ apps \ rerent \ src \ Rent \ ProgramBundle /../ Tài nguyên/skeleton/crud "thư mục không tồn tại. Tôi đoán nó cố đọc từ gói thay vì từ ứng dụng/thư mục. Cứu giúp? – Zeljko

+0

Quên để cho bạn biết một trong những caveat của phương pháp này, bạn phải sao chép thư mục bộ xương để ProgramBundle của bạn. Ngoài ra '../' là không cần thiết. Mã đã chỉnh sửa. –

+0

Đã làm việc chưa? –

0

Câu trả lời của m2mdas làm việc cho tôi, nhưng chỉ sau khi phát hiện ra rằng nó nên đọc

Filesystem thay vì FileSystem!

Hãy xem nhà cung cấp/symfony /.../ thư mục Hệ thống tệp để xác minh điều này.

11

Để ghi đè các chỉnh sửa mẫu, ví dụ, trong phiên bản 2.3 trở lên, sao chép các tập tin:

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

cho thư mục:

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

Bây giờ, chỉ cần tạo crud với lệnh mặc định và nó sẽ sử dụng mẫu mới.

Các vấn đề liên quan