smzapp
smzapp

Reputation: 829

Proper way to put the twig files in Symfony Framework

I am new to symfony3 and so, please bear with me.

From the documentation and most of the tutorials I've encountered, twigs are to be put inside the app\Resources directory. Yes, I followed it.

Recently, I discovered this command on creating of a Controller, which is the fastest way I could:

php bin/console generate:controller --actions=showAction()/profile/:AppBundle:Profile:profile.html.twig`

When executing the above line, it automatically creates Profile Controller inside the src\AppBundle\Controller and twigs inside the src\AppBundle\Resources, note: Resources is a new folder created automatically.

Here is the directory:

enter image description here

From this point, I became curious what really the appropriate folder to store the twig files is. Should I copy and files back to app folder? Are there any reasons behind this?

Upvotes: 1

Views: 186

Answers (4)

Jason Roman
Jason Roman

Reputation: 8276

Most of the other answers are not giving you the full picture, so let's start with a little context.

There was an issue created on the SensioGeneratorBundle to update the Controller generator to use best practices, but it was closed and marked as a duplicate of another issue that updated the Doctrine CRUD generator. In there, the issue was fixed so that the templates use the app/Resources/views directory. I do not feel like the original issue should have been closed.

If you look at the pull request for the Doctrine CRUD generator, it changes the bundle directory path from $bundle->getPath() to $this->getContainer()->getParameter('kernel.root_dir'). However, the Controller generator was never similarly updated.

I believe you have stumbled across something that should be updated within the bundle itself, and that the original issue should have never been closed. I commented on that issue and might just submit a pull request myself to fix it.

So, to answer your original question, yes you should move them back over to the app/Resources/views folder in order to follow Symfony best practices, and eventually the Controller generator command should be updated in the SensioGeneratorBundle.

Upvotes: 1

mpiot
mpiot

Reputation: 1522

Both are correct, but if you want, in bestPractices (https://symfony.com/doc/current/best_practices/templates.html#template-locations), they says:

Store all your application's templates in app/Resources/views/ directory.

Upvotes: 1

Matx132
Matx132

Reputation: 163

both folder is correct, but Mostly app/Resources use to storage extends twig like layout or email, but inside Controller to use this controller. But twig from Controller use to antoher controller too

Upvotes: 0

Ajay Korat
Ajay Korat

Reputation: 701

Both ways are fine bcs if you create resources view inside app then it will be accessible in all bundles if you have to create only single bundle then inside bundle resources is also fine. So, Its depends on you. :)

Upvotes: 0

Related Questions