Symfony: Overriding/Adding routes from inherited bundle - symfony

Suppose i have a few sites that share around 80% of their functionality, including things like routing strategy, templates,etc.
I'd like to use the same codebase for all them, and have a "DefaultSiteBundle", and several "SiteABundle","SiteBBundle" overriding or adding functionality to the DefaultSiteBundle using bundle inheritance.
This means, the DefaultSiteBundle should always be loaded, and, depending on the domain name, load SiteABundle or SiteBBundle conditionally.
My problem is, how do i manage routes, without adding all SiteBundles to app/config/routing.yml?

The way i've solved it, in the end, was using a custom router, based on the Advanced loader explained in the symfony docs.The router loads and merges routes from both bundles.
It's also important to create separate caches for each site (overriding "getCacheDir" in AppKernel so it returns a different path for each domain)

Related

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.

Symfony2 templates/layouts

I am new to symfony2 so please forgive me if this is a stupid question.
Symfony2 is organised in bundles - so everything is a bindle right?
Based on this I have created the following bundles in order to have a simple login mechanism:
App
The main bundle will contain all global functionality
User
Will be used to represent users
So the bundles work correctly and all is fine.
Now I cant figure out the best way to add a layout/theme structure to the site.
I obviously need some global assets such as header, nav and foooter. But additionally, there needs to be some global css style sheets, jquery etc.
The most obvious place bundle is App - but how do i make all other bundles inherit the theme from this bundle. For example, the user bundle template needs to extend the App bundle etc.
The idea for bundles is that they are modular and self contained, therefore how can this be achieved
I personally use like this:
There is MainBundle (in your case, it is App) which does global services, twig extensions and layout. Global assets are included in this file.
Main layout of all the other bundles extends the layout of MainBundle. Templates inside each bundle extend to the main layout of it which extend the layout of MainBundle. For example,
- MainBundle
- views
- layout.html.twig
- UserBundle
- views
- layout.html.twig (extends to MainBundle/layout)
- show.html.twig (extends to UserBundle/layout)
- friends.html.twig (extends to UserBundle/layout)
Everything is explained in the official docs: http://symfony.com/doc/current/templating.html
The basic global view (templates) resources are placed in the following dir:
app/Resources/views/index.html.twig
If you have a specific purpose, or bundles, templates, place them in the subdirs, e.g.:
app/Resources/views/blog/index.html.twig
And if you want to keep everything in the bundle (must for reusable code), use this convention:
[VendorName/]YourBundle/Resources/views/Blog/index.html.twig
(Of course, any name, except the ".html.twig" extension for Twig, may be changed to your liking)
The strategy I prefer is to organize my application in a single bundle. If you have no intention of re-using distinct standalone features across multiple applications then this is the most appropriate way. Having a "UserBundle" in your own application namespace probably doesn't make sense. You're adding a lot of extra structure that's serving you no advantage. Consider this instead:
- MainBundle
- Controller
- UserController
- OtherController
- Resources
- views
- layout.html.twig
- User
- show.html.twig
- update.html.twig
- friends.html.twig
- Other
- some_other_view.html.twig
In this case the templates under the controller directories would extend MainBundle::layout.html.twig.

Default ASP.NET Themes

Is it possible to create a default theme for an ASP.NET Website?
For example, If I had a theme called "Default", and ive selected a theme called "NewTheme" and I referenced a file which doesn't exist in the "NewTheme" but does exist in the "Default" theme like:
<asp:image id="img" runat="server" ImageUrl="~/Images/image.jpg" />
Could that then be taken from "/App_Themes/Default/Images/image.jpg" if it does not exist at "/App_Themes/NewTheme/Images/image.jpg"?
Furthermore if a CSS class didn't exist in "NewTheme", but it did in "Default", then could it take the "Default"? In fact, I think it would be better if it first took all the default styles, and then overrides any that "NewTheme" have which clashes.
I know Global References work similar to this because if ive selected "es" localization, and a key doesn't exist in the webreference.resx.es file but it does in webreference.resx, then itll take the value from there.
I think this would be important functionality for ASP.NET Themes as I can imagine different themes only having certain images changed, and certain styles changed. I can't imagine every image and every style always being totally different for each Theme. And therefore without this functionality, its going to be a case of duplicating styles/images, which I'm not a fan of (for obvious reasons!).
Default themes as you describe aren't supported by ASP.NET. There are regular Themes and StyleSheetThemes, but changing them dynamically is more useful at the Page request level than for individual Controls or static files.
You can code up your own version of themes for static files using URL rewriting or routing -- but then it's not really Themes any more.
For controls like <asp:Image>, you could override them and modify properties such as ImageUrl based on which files exist in some hierarchy of "theme" folders. Then use tag mapping to replace all instances of that control with the new one, without requiring any markup changes.
FWIW, the BeginRequest event in Global.asax is only invoked for dynamic files in IIS (Cassini calls it for statics, too). To support statics in IIS, you'll need an HttpModule, and you'll also need to configure IIS to run in Integrated mode.
This functionality isn't built into ASP.NET. Nevertheless, you could implement it fairly easily:
Hook the HttpApplication.BeginRequest event in Global.asax or in a custom HTTP module.
Look for requests with URLs under "/App_Themes/NewTheme/".
Check whether the file at HttpRequest.PhysicalPath exists.
If the file doesn't exist, call HttpContext.RewritePath and replace "NewTheme" in the request URL with "Default".

symfony 2.0 bundles interworking

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).

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