我试图通过创建覆盖 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.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
中覆盖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/