Where do you put your homepage in Symfony2 Project? - symfony

As Symfony2 said, everything in the framework is bundle. So, your website will also be a bundle. As far as I could understand from http://symfony.com/doc/current/cookbook/bundles/best_practices.html, bundle represents a feature, and that means everytime I want to add a new feature, I must create a new bundle.
For example, I want to create a website which have Blog features and Event Features. Then I will have 2 bundles, BlogBundle and EventBundle. Every code related to Blog will be housed under BlogBundle, and Event in EventBundle.
My question is, where should I put the homepage? or maybe another page like Contact, About, Terms and Policy, etc...?

See these two questions and my answers to them:
Symfony2 conceptual issue: general bundles vs. specific ones
Should everything really be a bundle in Symfony 2?
TL;DR
Create one app-specific bundle called AppBundle to avoid the hardcore decision making process on where to put and then find an entity.

Related

How to organize templates in Symfony

I have an application build with Symfony 3.3 and Twig that will be distributed to more customers. I'm using parameters.yml to customize its behavior and it seems to work well.
Where I have an issue is with twig templates: although the customers will use most of the templates as they are, they'll need to customize some parts like CSS styles, general layout and do occasional design overrides.
The options I have identified are:
Have a full set of templates for each customer. The issue is that the upgrades will be a nightmare as we have to patch each template and account for differences
Customize templates via YML files. The problem is that it gets too complicated soon and the number of parameters is potentially huge
Deliver a set of templates in app/Resources/views and allow the customer to override any of the templates by creating another file with the same name in another folder
Deliver a set of templates in AppBundle/Resources/views and let customers override them in app/Resources/views
Create the application as a Symfony bundle (ie. MyAppBundle) and deploy the application to each customer by including the MyAppBundle via composer. I like this solution a lot, but I do not know whether it is possible to implement is easily.
Do you have any suggestion on how to approach this problem?
I think the best way in your case is to think about the main parts and the customer parts and then build blocks and overwrite all parts that are relevant for the customer for example the CSS or anything else. If you know that there are special part you can build macros for example.
With the Block concept you can predefine the template but the customer has the ability to overwrite that part you've defined with it's own.
With Twig you have a lot of possibilities to solve such a problem.
Drupal 8 use that Theming concept.
https://www.chapterthree.com/blog/twig-concepts-drupal-8-themes-part-ii
Perhaps you can take a look how they have solved that and you can find a good solution for your problem.

Symfony2 Using an Entity Manager within Twig extension

I'm creating a menu system using the Tree Doctrine extenion and I want to create a Twig extension to display the menu based on the requested parent node e.g. {% display_menu(side_menu) %}. This function will be in the base twig template (i.e. the menu is on every page of the website).
As I'll be storing the Menu structure with Doctrine, I thought I'd need to access the MenuRepository within the Twig extension so the first problem I came across was getting an Entity Manager into it. When looking for a solution, a few people have said that this is bad practice, which makes sense as a Twig extension is part of the View.
So although there is a solution (linked in similar questions) to my problem, my question is, is there a way I can accomplish it using good practice? Or is there a better way of doing it in the first place?
Making entities aware of any services — including entity managers — is a bad practice. There's nothing wrong about injecting an EM into a Twig extension. Although, I'd rather inject a model manager to a Twig extension, so that the extension is not aware of the persistence layer — it's the job of the manager layer.
So, I'd have MenuManager that's aware of repositores/entity managers and inject it to an extension.

Symfony2 Bundles [duplicate]

Oke so I am about to make a website using symfony 2.
Should I just make a "main" bundle that controls/puts together all other bundles?
With other bundles I am thinking of lets say a "gallery" bundle that controls things related to photos, and a "store" bundle that controls a shop part.
What would be best (or atleast good) practice and how would professional teams do it?
According to symfony documentation a bundle should be consistent and closed structure. So, if for example "store" and "gallery" are related in some way (eg. use the same model), then they should be in one bundle (AppBundle, CoreBundle, PlatformBundle - whatever you want). But if gallery is completely separate piece of code and can be easily join to another project - then you should consider exclude it to separate bundle.
I think a good idea is look at some projects on github and look how others handle this.
See the following questions and my answers to them:
Should everything really be a bundle in Symfony 2?
Symfony2 conceptual issue: general bundles vs. specific ones
Basically, in my last projects I'm not using bundles for app specific code; the only exception is for stuff that's hardcoded to be in a bundle — like Doctrine Fixtures — that I put in AppBundle. Everything else — models, controllers, services, form types, etc — are out of any bundle.
Like #Cyprian said a bundle is a set of functionality that can work alone. As it happens while developing we do not always know when things are separate. It comes with time.
Personally, I am working with Symfony2 since Febuary and I never stopped reading the manual and related books to understand more in depth. That helped a lot and is very interesting read, I assure you :)
Here is my top favourites documentation pages, en enlightening blog posts on delicious.
For your immediate question, forget about "frontend" and "backend" as we were doing in symfony 1.x. Just think of Model entities (as in A SINGLE row) and build in one bundle. As your code will grow, you will see how to disassemble and separate in bundles. You just have to keep in mind to separate your functionnalities in small methods and refactor around.

For what would I make a bundle? (Symfony 2)

Oke so I am about to make a website using symfony 2.
Should I just make a "main" bundle that controls/puts together all other bundles?
With other bundles I am thinking of lets say a "gallery" bundle that controls things related to photos, and a "store" bundle that controls a shop part.
What would be best (or atleast good) practice and how would professional teams do it?
According to symfony documentation a bundle should be consistent and closed structure. So, if for example "store" and "gallery" are related in some way (eg. use the same model), then they should be in one bundle (AppBundle, CoreBundle, PlatformBundle - whatever you want). But if gallery is completely separate piece of code and can be easily join to another project - then you should consider exclude it to separate bundle.
I think a good idea is look at some projects on github and look how others handle this.
See the following questions and my answers to them:
Should everything really be a bundle in Symfony 2?
Symfony2 conceptual issue: general bundles vs. specific ones
Basically, in my last projects I'm not using bundles for app specific code; the only exception is for stuff that's hardcoded to be in a bundle — like Doctrine Fixtures — that I put in AppBundle. Everything else — models, controllers, services, form types, etc — are out of any bundle.
Like #Cyprian said a bundle is a set of functionality that can work alone. As it happens while developing we do not always know when things are separate. It comes with time.
Personally, I am working with Symfony2 since Febuary and I never stopped reading the manual and related books to understand more in depth. That helped a lot and is very interesting read, I assure you :)
Here is my top favourites documentation pages, en enlightening blog posts on delicious.
For your immediate question, forget about "frontend" and "backend" as we were doing in symfony 1.x. Just think of Model entities (as in A SINGLE row) and build in one bundle. As your code will grow, you will see how to disassemble and separate in bundles. You just have to keep in mind to separate your functionnalities in small methods and refactor around.

Symfony bundle composition, how do I structure my code when using multiple bundles

I'm looking for some guidance on best practice around how to structure my Symfony 2.0 application. If I have a few bundles (Cart Bundle, Product Bundle, CMS Bundle) and I wish to use aspects of all of these bundles when composing my page how should I best do this?
I can imagine two ways to do this, but am looking for guidance on which (if either) is correct.
1) Expose all of the functionality of my bundles through services, and have these services available to use directly from within twig. This way I can pass my routing request to the most appropriate bundle (so http://myclient.org/User/Account) is passed to the ClientUser bundle to handle, but the base template which has a mini shopping cart in the navigation is able to access the information it needs directly from within twig (I don't need to pass this in)
2) Create a bundle that accesses all the other bundles in order to build up the page (like VendorFrontend or VendorBackend). This would mean that ALL routing requests would be passed to this bundle and this bundle would access the required information for every part of the page before passing off to the template.
The first option feels wrong because I'm not sure if it's even possible to allow Twig to consume services directly (though the service container)?
Second option feels wrong because It's like using a second router, the routes would be being passed into a bundle, which only exists to compose the other bundles (it's a given here that this bundle is tightly coupled to the bundles that it uses). Surely this goes against the concept of a 'bundle' that the code is reuseable.
In this example I'm trying to build a very simple e-com site for demonstration purposes only. I have a base template which will have a primary navigation, mini shopping cart, 'body' and footer. I'm storing this in the /app/Resources directory. I was planning on having all other templates inherit this one and overriding the 'body' area.
Not looking to be spoonfed, just a nudge in the right direction. Thanks.
I think that it's important to get away from the idea that, in order to generate a "page", one has to corral together all the variables that the templating involved might need in order to render, and then pass these into a template. A controller should be doing only the specific thing for the request it's serving, and no more. So if there's a specific product referenced in a URL, fetch the product and pass it into the template. If there's a specific product referenced but it doesn't exist, or shouldn't be shown, you respond with a 404/ 410/ whatever is appropriate. If there's a specific collection, fetch the collection and pass it in. The routing/ controller should be decoding the request - the URL itself, the HTTP method, etc - and translating that into something specific. Anything general and not specific to a particular request doesn't belong there.
It's also important, I would say, to abstract as best you can the bundles you use from Twig templates. I am advocating more that templates "pull" what it needs into them, rather than being pushed in, but this is achieved by the definition of Twig functions within bundles that can themselves, via the DI container, hook into data that may or may not be in the current request, etc. So you make a Twig function that can take as a parameter anything that might change - if it has to do with product categories, let it take a product category object as a parameter.
Essentially the answer is more 1) than 2), but you shouldn't be accessing services directly through Twig - you should be proxying via functions that make semantic sense within the template, that themselves are defined to have services injected into them at runtime, services that you are at liberty to define differently in any new bundles you include or write.

Resources