Doctrine 在自定义包中创建具有CRUD原则的视图

Doctrine 在自定义包中创建具有CRUD原则的视图,doctrine,crud,symfony,Doctrine,Crud,Symfony,当我启动这个命令时 php bin/console generate:doctrine:crud --entity=CustomBundle:Test 在内部创建相对视图 symfony/app/resource/views/test 但是我的CustomBundle和相关实体是在里面创建的 symfony/app/src/CustomBundle/Entity 所以,问题是:如何在内部创建这些视图 symfony/app/src/CustomBundle/views ? 你应该在那里找

当我启动这个命令时

php bin/console generate:doctrine:crud --entity=CustomBundle:Test
在内部创建相对视图

symfony/app/resource/views/test
但是我的CustomBundle和相关实体是在里面创建的

symfony/app/src/CustomBundle/Entity
所以,问题是:如何在内部创建这些视图

symfony/app/src/CustomBundle/views
?


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

这个命令没有任何选项。您可以在中看到所有可用的选项。但是如果您真的需要它,那么您必须覆盖SensioGeneratorBundle,您可以看到如何在中扩展任何bundle。然后重写php类vendor/sensio/generator bundle/generator/DoctrineCrudGenerator.php,然后查找操作 “生成”并定位此行:

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

$dir = sprintf('%s/Resources/views', $this->rootDir);
我还没有试过,但理论上应该行得通。由于您打算在其中创建所有视图,因此可能需要覆盖每个生成视图的名称,您可以在该类中看到下面每个视图的操作

希望这对您有所帮助。

您需要改变:

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(

正如我在问题中所写,我在customBundle中找不到该视图。我在symfony的视图目录中找到它,在symfony/app/resource/views/test下(PS:视图工作正常,我使用的是symfony 3)我使用的是Symfony 3.0.4-DevKey,所以当你运行服务器时,它工作了吗?它说什么也许我必须在config.yml中声明一些东西?当然,你应该将其更改为数据库设置ps。yml非常敏感,所以当你尝试更改任何内容时,不要添加特殊字符,甚至空间,它不会运行,但如果crud生成时没有问题,那么它应该可以工作,即使路径不一样,也可能是因为你没有放置数据库,为什么它没有像应该的那样反应我说不出来,因为你没有提到你问题中的错误不起作用:新视图现在位于symfony/app/resource/views/test这太奇怪了:当我创建一个捆绑所有工作正常。控制器和实体存储在右目录中。视图不是…问题是每个实体的控制器具有不同的名称,但所有显示视图都命名为show.html.twig,因此,每个实体需要位于不同的文件夹中。这一行为似乎在3.x中发生了更改,默认情况下,视图存储在ge中一般资源/视图。您能试试$dir=sprintf('%s/Resources/views',$this->bundle->getPath()吗;这是工作…但是…没有创建目录。现在我可以看到MyBundle/Resource/views/中的四个视图,但是这些视图不在MyBundle/Resource/views/Product中。因此视图不划分在目录中,CRUD命令也不创建它
return $this->render('{{ bundle }}:{{ entity }}:edit.html.twig', array(