php - Symfony2 : how to override core template?

标签 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 这样的新包的情况下做到这一点?

最佳答案

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

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

关于php - Symfony2 : how to override core template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261333/

相关文章:

php - 导出 Sonata Admin 时的自定义列名称

php - 如何在 Symfony2 服务中获取当前的相对 URL?

php - 仅自定义 Symfony 表单的数组元素/片段

php - 更改每个 session 的 MySQL 模式

php - mysql_connect - 连接到 ISP 而不是远程主机

php - 重新排列对象PHP

javascript - 如何让 PhpStorm 知道定义了一个 javascript 全局变量?

javascript - 返回页面时 PHP 搜索结果

php - 我的 Laravel 应用程序在 Google Cloud 上有问题。无法打开流或文件 "/srv/storage/logs/laravel.log"

php - 我如何使用 PHP 读取 .xls 文件 (Excel)?