i18n not working on custom error pages - symfony

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.

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.

template twig not found

I have a problem with a render function in my controller.
I have:
a bundle, named CoreBundle
a controller named DefaultController
in which I have an action named loginAction
in which I am trying to render a template twig with this line
return $this->render('FooCoreBundle::Default:layout.html.twig', array());
I am always having the same not found error for the template. I have tried with an other template twig it is the same problem. In my views folder I have a Accueil folder in which i have put the login.html.twig template I want to render.
I tried to replace it directly in views folder but no changes. Sorry for my english hope you can help. Thank You.
A worth-while reading piece of info from the documentation:
Symfony uses a bundle:directory:filename string syntax for
templates that live inside a bundle. This allows for several types of
templates, each which lives in a specific location.
Supposing:
FooCoreBundle is your bundle -> src/Foo/CoreBundle
Default is the template directory -> src/Foo/CoreBundle/Resources/views/Default
layout.html.twig is your file -> src/Foo/CoreBundle/Resources/views/Default/layout.html.twig
Then the route you should use would be 'FooCoreBundle:Default:layout.html.twig' without the :: you're using in 'FooCoreBundle::Default:layout.html.twig'
The :: refers to a base template specific to the bundle. If you used FooCoreBundle::layout.html.twig then your template should live at Resources/views/layout.html.twig inside FooCoreBundle.
Hope this clarifies things.
You should use
return $this->render('NeobeCoreBundle:Default:Accueil:layout.html.twig', array());
No double colon, add the Accueil subfolder
It is not really an answer, but it could be handy for those who arrives here with symfony 4.
In documentation authors says do not organize your business logic into bundles.
In modern Symfony applications, it's no longer recommended to organize your business logic using bundles.
Of course you can, just not recommended. See here.

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.

Adding functionality to a wordpress plugin

This is a simple concept question... If I have a wordpress plugin and I want to write some functions for my own IN ADDITION to what they have already where would I put them in the wordpress file structure?
I cant put them in the original core.php files/directory because they will get wiped out when the plugins are updated right?
#NomikOS - Yeah Sure .. I have a wordpress plugin called BP-Phototag- pretty much an album..Its located in the wp-content/plugins folder. I want to add my own functions...for a specific template in my themes folder. DO i put the functions in the bpa.core.php file in the wp-conten/plugins folder or do I make a new php file that can inherit bpa.core.php functions(which I dont know how to do) and stick them in my template specific folder under wp-content/themes/mytheme folder. Im really not sure how to extend and override it...
If the plugin is class based, you can extend it to override/add methods. You can include the file containing your code inside plugin's directory if you want (it will not deleted after an upgrade) or directly inside plugins dir.
EDIT 1
Sorry, I didn't saw your last comment. Well, my friend, it's time to learn OOP PHP5. I recommend you PHP 5 objects, patterns, and practice. It's for PHP serious coders.
Basically you do
class leon_my_class extends BP_Phototag_class {
function __construct()
{
parent::__construct();
// my code
}
// overriding protected/public method
function BP_Phototag_method()
{
// this code will replace original code
}
// adding method
function my_own_method()
{
}
}

Resources