I'm coming from the ASP.NET MVC world where my folder structure would be:
+ProjectName
-App_Data (databases, xml's and such data)
-App_Start (function bundles, routes, etc.)
-Controllers
-SampleController.cs
-Models (though I'd usually call models from other projects)
-Views
-Sample
-index.cshtml
-Scripts
web.config
etc.
I'm trying to get into PHP Symfony2, right now. I've read some of the documentation and read a tutorial or two, none of which answer this one question that I keep asking myself.
From my understanding bundles are pieces of the web application that I can utilize in the current project as well as other projects I make. They, as one, fill a certain web application function.
I used the command to generate a bundle:
$ php app/console generate:bundle --namespace=Sample/FooBundle --format=yml
and I've got some folders and sub-folders and whatnot, but also a Resource folder, which is what confused me.
So what does the web application read first? How does the controller search for the needed view? Does it first go to
src/Sample/FooBundle/Resources/views/Default/index.html.twig
or does it go to
app/Resources/views/default/index.html.twig
Is it smart to keep the views in bundles, or in the 'global' app folder? I've also noticed configurations are being made inside the bundle (which, I suspect have a higher hierarchy than the app 'global' ones). For instance, bundle AppBundle
doesn't have its own views and configurations, which made me wonder if it's smart to make views inside bundles in the first place.
If I could get a simple answer, or at least a resource about it that could explain the reason behind all of this, I'd be very thankful.
By and large, anything within a Bundle can be overridden by "global" application settings and resources. Probably the most useful resources for understanding this are the cookbook entries How to Override any Part of a Bundle and How to Use Bundle Inheritance to Override Parts of a Bundle.
To answer your question of whether to keep views in the bundle or global folder - it all depends on whether the views make sense within the bundle itself. If something is going to be used in multiple bundles (e.g. a layout base template) then that makes more sense in the app
folder.
In terms of your bundling structure - this has changed since I started with Symfony so I'm not an authority on this. The trend nowadays seems to be that if you can split the functionality into a self contained bundle, do that, otherwise pop it in the AppBundle
that's by default created by Symfony installer. You might want to have a look at Best Practices for Reusable Bundles for further info.
It is entirely possible to have multiple application bundles with things like entities and resources stored outside of the bundles, e.g.
src/Company
Bundle/
ManagerBundle/
FrontendBundle/
Entity/
Event/
Whether you do this is entirely up to you - your code, your organisation, beyond a few specific instances, Symfony as a framework won't complain about this.
As for bundle configurations being higher in the "hierarchy" than global - usually no though this is tied in with the DI component. It's a lot to get your head around, but the Cookbook "Configuration" section is a great place to start.