symfony 2.0 bundles interworking - symfony

I want to make an application based on a main bundle leaving the possibility for other developers to make their own bundles to implement other features.
Symfony 2.0 seems a good choice for that however I cannot figure out how to let the bundles to work together while preserving the decoupling.
In the MainBundle I will create a Controller which generates a Users-List like the one below:
user1 edit remove
user2 edit remove
....
How to let third-parties bundles to add their custom buttons to this list?
For example an AvatarBundle may want to add a button to upload of the Image, a SendEmailBundle my want to add a button to send an email to the user and so on.
How to preserve the bundle independency? How can I do that?
Thanks a lot,
Massimo

As far as I know, there are only two ways of changing/adding functionality in provided bundles.
Change the code
Overriding the template/controller
In this case, the second seems far preferable.
The ways to override a template are:
Define a new template in app/Resources
Create a child bundle and override the template
If you also want the override controllers, the second way is the only way to go.
It's also my personal preference, since it's cleaner then putting specific stuff in the general app-folder, in my opinion.
Anyway, it is far better explained in the documentation of the FOSUserBundle:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md
And ofcourse, this cookbook article:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html
Hope this helps,
Dieter

If you know which parts of your bundle will be extended, you can use services (in the same way the tiwg bundle is using them for adding new templating filters/tags/helpers).
Your bundle will then 'scan' the services defined in the DIC (which have a defined tag) and then call them to get informations (button definitions in your case).

Related

Correct way to override default template in Sonata

I'm a bit confused as I've seen a few varying methods posted online.
I have a bundle created with easyextends in src/Application/Sonata/SonataMediaBundle, which extends the SonataMediaBundle in the vendors.
The default template displays
This is the gallery index template. Feel free to override it.
This file can be found in SonataMediaBundle:Gallery:index.html.twig.
so I've added src/Application/Sonata/SonataMediaBundle/Resources/views/Gallery/index.html.twig in my bundle
and this works and overrides, so why all these various other ways like How to override Sonata Media Bundle templates?
You are using the correct way. To quote from symfonys How to Override Templates from Third-Party Bundles
To override the bundle template, just copy index.html.twig template from the bundle to app/Resources/AcmeBlogBundle/views/Blog/index.html.twig (the app/Resources/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to customize the template.
And for more detailed/ complex overriding behaviour have a look at How to Use Bundle Inheritance to Override Parts of a Bundle
I wouldn't worry to much about other solutions unless you are not able to get the results you require using this method but I cant think of one.

Location for Symfony bundle templates

The directory structure for bundles specifies that views should be stored in <your-bundle>/Resources/views. But then the best practices for template locations says that, actually, I should store them in app/Resources/views. I can see the conveniences of doing the latter, but I don't understand:
If I am organizing things into bundles, aren't I reducing the bundles' portability by "de-bundling" the views?
The examples show that index.html.twig is easier to write than AcmeDemoBundle::index.html.twig, but what if I also have FooBundle::index.html.twig? I still need a way to specify which index.html.twig I want, right?
The documentation is correct. Store reusable bundle's templates in <your-bundle>/Resources/views and your project's templates in app/Resources/views. There is no conflict.
If you decide to create a bundle make sure it can be reused (that's the main purpose of the bundle). Otherwise keep using your AppBundle. AppBundle will not have two /index.html.twig's.

Payum Bundle : How to change the view of capture action in symfony2

I am using PAYUM Bundle for the payment gateway integration, and its basic example is working fine, But now I want integrate the payum bundle in application by changing the payum capture action layout and adding extra field payment detail entity.
PAYUM BUNDLE with AUTHORIZE.NET GATEWAY.
Please can anyone help me out.
Thanks all in advance.
The payum templates is not kept in the bundle but in the payum lib itself. Standard templates inheritance does not work here.
There is no simple way to do so in version 0.9 (which is shipped with Sylius right now). You have to overwrite the whole CaptureAction class. In 0.10 it is possible to change templates by overwriting container parameters like for layout, or stripe js page.
In the sandbox you can find an example of layout modification
You have to override views of PayumBundle.
How to do that?
You have to reproduce the folder structure of the bundle you want to override inside your /src/Path/To/Your/Bundle/Resources/views and place inside it the twig file you want to override.
Example
Let's say your bundle name is FooBundle and you want to override PayumBundle payment.twig.html (I'm just making an example, don't know if there's a file named that way). Let's say, also, that this twig is inside /vendor/Path/To/PayumBundle/Resources/views/Payment/payment.twig.html.
What you have to do is create inside /src/Path/To/Your/Bundle/Resources/views/Payment/payment.twig.html
Symfony2 will look, at first, into your bundle for overrided views: if any will took yours and ignore bundles one. Otherwise It will took bundle's one.

Extending all templates in a bundle from the controller

I have a lot of templates in a bundle and they all extend the same layout.html.twig. Rather than have to define:
{% extends 'MyBundle::layout.html.twig' %}
at the top of every template, is there a way to do configure this?
I suspect that I would need to create a pre- or postExecute()-like method based on an event listener that extended the template before rendering.
This is not a good idea, because not every template has to extend a layout: there are some “system” templates like form_div_layout.html.twig that are not meant to extend anything. Besides, not every template you write has to extend a layout; you'll encounter a lot of use cases for small embeddable templates to reference from other templates.
If you will try to force a layout on each template, you'll have to write some logic to exclude “system” and embeddable templates so that they don't extend anything, and you'll have to do the same for your layout template too, so that it doesn't extend itself infinitely. In this case you'll reverse the problem: instead of defining explicitly which layout to extend in each template, you'll have to explicitly state which templates should not extend your layout. This will get very messy very fast.
To grasp this idea more completely, you need to know that, behind the scenes, templates are really just PHP classes. Does it make sense to you to make a particular class the parent for every other classes, and then explicitly state which classes should not extend this parent?
But if I haven't convinced you not to go this way, there is a Twig setting which lets you set the base template class for all templates:
twig:
base_template_class: Your\Layout\ClassName\Here
You can extend \Twig_Template or implement \Twig_TemplateInterface and have some “fun” for several hours, after which I hope you'll be convinced to abandon this idea whatsoever. Good luck. :)

What goes in app/Resources and what goes in Acme/MainBundle/Resources

I am starting a new project using Symfony2. I am adding some basic twig templates like my CSS template (with assetic to trigger sass -- nifty) and my header/footer template. My question is, should this sort of thing go into app/Resources or into Acme/MainBundle/Resources?
I am sure I can do either if I really wanted to, but I'd like to know what the correct way to do it is.
app
Resources
<A. here?>
src
Acme
MainBundle
Resources
<or B. here?>
It's really a matter of preference. I tend to keep global views in app/Resources. Does your MainBundle contain anything else besides views? If not, I think that's more reason to use app/Resources and avoid unnecessary bundle pollution.

Resources