在自定义 Bundle 中使用 CRUD Doctrine 创建视图

Create views with CRUD Doctrine inside a custom Bundle

当我启动这个命令时

php bin/console generate:doctrine:crud --entity=CustomBundle:Test

Doctrine 在里面创建相对视图

symfony/app/resource/views/test

但是我的带有相关实体的 CustomBundle 是在里面创建的

symfony/app/src/CustomBundle/Entity

所以,问题是:如何在内部创建这些视图

symfony/app/src/CustomBundle/views

?

symfony/app/CustomBundle/resources/views/test 

你应该在那里找到它,这取决于你使用的是哪个版本ps你标记的 symfony 3 和 symfony2 所以 !!!

好吧,该命令没有任何选项。您可以在 official documentation. But if you really need that then you must override the SensioGeneratorBundle, you can see how to extend any bundle in here 中看到所有可用选项。然后重写phpclassvendor/sensio/generator-bundle/Generator/DoctrineCrudGenerator.php,然后找到action "generate" 并找到这一行:

$dir = sprintf('%s/Resources/views/%s', $this->rootDir, str_replace('\', '/', strtolower($this->entity)));

替换为:

$dir = sprintf('%s/Resources/views', $this->rootDir);

我没试过,但理论上应该可以。由于您打算在其中创建所有视图,因此您可能需要覆盖每个生成的视图的名称,您可以在下面的 class.

中看到每个视图的操作

希望对您有所帮助。

您需要更改:

yoursymfonydir/vendor/sensio/generator-bundle/Generator/DoctrineCrudGenerator.php:

   public function generate(.....) {
   .....
   /*comment this line*/
     //$dir = sprintf('%s/Resources/views/%s', $this->rootDir, strtolower($entity));
   /*replace by*/
     //Create the views inside the bundle :
     $dir = sprintf('%s/Resources/views/%s', $this->bundle->getPath(), $entity);
   .....
   }

并且在 yoursymfonydir/vendor/sensio/generator-bundle/Resources/skeleton/crud/actions/edit.php.twig, index.php.twig, new.php.twig, show.php.twig 中,您需要以这种方式在每个文件中使用 render( 更改行:

return $this->render('{{ entity|lower|replace({'\': '/'}) }}/edit.html.twig', array(

替换为

return $this->render('{{ bundle }}:{{ entity }}:edit.html.twig', array(