inheritance with bundles changes the routes? - symfony

I have just created a new bundle and its route : it works fine.
Now I would like this bundle to inherit from another bundle I have : I add this method to the bundle class :
public function getParent() {
return 'DemoAsseticGestionRessourcesBundle';
}
and suddenly Symfony does not recognize the route and says that it does not exist anymore (the same route).In fact it uses the parent route to go to the new bundle I created and so I cannot use the parent bundle anymore.
Why ?
How should I do ?
(I would like to keep the routes to their owner.)
Note : I use yml
Thanks to you all
Note2: my routes
demo_gestion_bundle_homepage ANY ANY ANY /GestionBundle/
demo_assetic_gestion_ressources_homepage ANY ANY ANY /AsseticGestionRessources/
So to be clear : when I do /GestionBundle it does not work anymore and when I do /AsseticGestionRessources it leads to the GestionBundle

Check your routing.yml file paths. If these have the same paths, you overwrite the parents routing.yml file.
So if your parentBundle has the routing.yml file located at src/ParentBundle/Resources/config/routing.yml and the childBundle has the routing.yml file located at src/ChildBundle/Resources/config/routing.yml, it's not gonna work.
It's nice if you post your app/console router:debug output here

Related

Symfony include static html in template

I just started to learn how to use Symfony which i believe is straight forward but i have a little problem with the templating engine. I want to include a static HTML fragment in one of my twig templates in Symfony (2.5.6). For now i created a static subfolder inside the resources directory (this might change but it definitely won't be inside the view folder). However i can't get this done, i always end up with an unable to find template error. I find the documentation on this subject a bit sparse (see http://symfony.com/doc/current/book/templating.html#including-other-templates) also the twig docs can't help me out on this one. I'm not even sure if i can use the magic #Bundle notation or if i have to reside in the view folder as ../ notation is not allowed within the include tag.
I tried the following (and a couple of variations):
{{ include('#FashionMediaBundle/Resources/static/pricing.html') }}
I guess symfony cannot handle raw html in include but i could use a php template without any template tags as well, so the question is just how to specify a location outside the view folder.
I know of http://github.com/kgilden/KGStaticBundle which will probably solve my issue but i can't believe that is not achievable with the default configuration.
EDIT: i just tried to include a regular template file from the very same directory specifying just the name of the template file (as done in the docs, see link above). Still i get an error also complaining it expects bundle:section:template.format.engine as its format. Is there an error in the docs?
Looks like the poster found the solution while I was typing. I guess I'll leave this here for just a bit.
Creating and using twig namespaces is discussed here: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
By default, each bundles get's it's own namespace pointing to the views directory. You can add additional directories by adjusting app/config.yml
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
paths:
"%kernel.root_dir%/../../cerad2/src/Cerad/Bundle/FashionMediaBundle/Resources/static": FashionMedia
Load the template with: '#FashionMedia/pricing.html'
I won't go into all the details but you can also use a compiler pass(http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html) to add additional paths from inside the bundle itself without having to adjust config.yml:
class Pass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$bundleDirAction = $container->getParameter('cerad_api01__bundle_dir') . '/Action';
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($bundleDirAction, 'CeradApi01'));
}
}
Try this:
{{ include('FashionMediaBundle:Resources:static:pricing.html') }}
If you want to put this file in another directory I suggest you to use a symbolic link (I don't know if Twig can include a file which is not in the Resources directory).
I found the Solution!
In the twig configuration you can set paths and assign a name to them.
twig:
[... twig options here ...]
paths:
"%kernel.root_dir%/../[path to static]": static_pages
And then you can include them in
{% include "#static_pages/pricing.html.twig" %}

i18n not working on custom error pages

I've created templates for error pages in app/Resources/TwigBundle/views/Exception. But I have a problem with translations on these pages, i.e.
<h3>{{ 'error_page.404.header'|trans({}, 'ProjCoreBundle') }}</h3>
not working. My translations are in src/Proj/CoreBundle/Resources/translations/ProjCoreBundle.%lang%.yml, where %lang% means locale (i.e. "en") and they are working fine on other pages. What may cause a problem.
You need to make the necessary overrides in the proper directory. In this case in the top level directories app/Resources/TwigBundle/translations or in app/Resources/translations folder (see http://symfony.com/doc/current/book/translation.html#translation-resource-file-names-and-locations).
Symfony looks for message files (i.e. translations) in the following
locations:
the app/Resources/translations directory;
the app/Resources//translations directory
the Resources/translations/ directory inside of any bundle.
The locations are listed here with the highest
priority first. That is, you can override the translation messages of
a bundle in any of the top 2 directories.
To use a override parts of another bundle inside your own, you need inherit your bundle from the second one:
See http://symfony.com/doc/current/cookbook/bundles/inheritance.html
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
By making this simple change, you can now override several parts of
the FOSUserBundle simply by creating a file with the same name.
Also, if you're using Symfony 2.5 see the debug mechanism that were introduced in this version.

define custom filesystem path for twig templates

I've already read How does Symfony2 detect where to load templates from? and another ton of articles on the web
The question was quite similar to mine, but the answer wasn't comprehensive.
I need to override a bundle-defined template inside another custom bundle. In other words I want specify another path where symfony have to look before looking in app/Resources when loading templates.
Workflow should be something like this:
first: /src/My/Bundle/Resources/views/example.html.twig (or /src/My/Bundle/OriginalBundle/views/example.html.twig)
then: /app/Resources/OriginalBundle/views/example.html.twig
finally: /src/Original/Bundle/Resources/views/example.html.twig
The standard app/Resources -> src/Original/Bundle isn't enough.
sorry for my poor english and thank you
There's a native feature to do exactly what you want in a nice way. Escentially you can add a twig namespace to the twig configuration in app/config.yml like this:
twig:
# ...
paths:
"%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar
This creates an alias to the folder vendor/acme/foo-bar/templates and then you can use it to render your templates either from the controllers:
return $this->render(
'#foo_bar/template.html.twig',
$data
);
or from other twig templates
{% include '#foo_bar/template.html.twig' %}
Source: official cookbook http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
To add a directory for twig templates I do this:
chdir(__DIR__);
$this->container->get('twig.loader')->addPath('../../../../web/templates/', $namespace = '__main__');
this allows me to put twig templates in a folder called 'templates' in the web folder and symfony 2.3 has no issues loading them.
The class responsible for loading twig templates is stored at the service id twig.loader, which by default is an instance of Symfony\Bundle\TwigBundle\Loader\FilesystemLoader, and the class for locating templates is stored at the service id templating.locator and by default is an instance of the class Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator (which itself is injected as the first parameter to twig.loader)
So, in theory, all you would need to do is write your own class that implements Symfony\Component\Config\FileLocatorInterface (or extends Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator) and then inform the configuration to use your class (which I was going to look up how to do this for you, but Symfony's websites are down right now)

Symfony2 Error when overriding bundle template "resource is hidden by a resource"?

I'm trying to override a third party bundle's layout template with the layout of my bundle, by including the new layout in the app/Resources/ directory as indicated in the Symfony2 book section; however, I'm getting the following exception:
"...path_to_my_app/app/Resources/FOSUserBundle/views/layout.html.twig" resource is hidden by a resource from the "MyVendorMyBundle" derived bundle. Create a "...path_to_my_app/app/Resources/MyVendorMyBundle/views/layout.html.twig" file to override the bundle resource.
In particular, I would like to overwrite the FOSUserBundle layout with that in my bundle. I followed the steps shown in the bundle's documentation, which are no different than those in the Symfony book.
What could be the cause of this exception? and how can I make it work?
I tried putting my bundle's layout in the app/Resources/MyVendorMyBundle/views/ as indicated in the above exception message, but if I do that, only the layout of MyBundle is read and returned and not the templates for the FOSUserBundle that extend it.
Turns out the developer that preceded me had set MyBundle as a child of FOSUserBundle as shown in the documentation (without me noticing...):
// src/Acme/UserBundle/AcmeUserBundle.php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
The above was obviously creating conflict with (hiding) the re-defined layout template in the app/Resources/FOSUserBundle/views/ folder. I just deleted the getParent() method from the above shown bundle class to solve the issue.

Extending parent routing when extending bundles

In Symfony2 (2.0.3) I have a BetaBundle that is set as the parent of a AlphaBundle. Is it possible to override some routes while still keeping the parent originals routing definition ?
I have tried importing the parent routing.yml inside the child routing.yml file but it naturally result in a circular reference exception.
Is there any standard way to achieve this using yml and files named routing.yml in the same relative path ?
When you override your AlphaBundle anything that uses the #AlphaBundle shortcut will look in #BetaBundle first. The only way I've found to solve this kind of problem is to have the extending bundle (BetaBundle) quit using the # shortcut, and include your AlphaBundle's route using it's path.
As an alternative you could try renaming your BetaBundle's route file so that it doesn't override AlphaBundle's file, and then configure your app/config/routing.yml to include your BetaBundle's renamed routing file.

Resources