I have a problem with generating relative URL in symfony 2.7 project
My project is hosted behind a proxy that redirect URL to the target VM, like this - example.com/oblounge/bo/ to vmhostname/
All my URL are generated in relative
Here is my problem
For example when I'm on this page
http://example.com/oblounge/bo/admin/articles/actualite?section=127
All generated likns are malformated, the "/admin/articles" is duplicated
http://example.com/oblounge/bo/admin/articles/admin/articles/actualite?section=127
Note: The problem is occurred only for rendered controllers
The render action
{{ render(controller('AppBundle:Back/Article/Article:sectionsArticle', { 'rubric': 'actualite' })) }}
The rendered twig
{% for section in sections %}
<li><i class="fa fa-circle-o"></i>{{ section.name }}</li>
{% endfor %}
My routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
The prefix of my admin controller
/**
* #Route("/admin")
*/
class ArticleController extends BaseArticleController
{
I would suggest you give a look at your routing.yml file that is referenced within your bundle.
If you've already defined a prefix for the route in the routing.yml of you app/config there is no need to repeat it in the routing of your bundle , otherwise there will be a duplication of prefixes as in your case.
Example:
This is the routing.yml in the App\config folder
tutoLexikTestBundle_parents:
resource: "#tutoLexikTestBundle/Resources/config/routing/parents.yml"
prefix: /parents
And this is the one of the bundle
parents_show:
path: /{id}/show
defaults: { _controller: "tutoLexikTestBundle:Parents:show" }
I don't need to repeat the prefix 'Parents' in the path
Related
I'm trying to build Login form popup and I want to render LoginAction controller in twig.
I'm getting error like:
An exception has been thrown during the rendering of a template ("Template is not configured.").
My twig template:
{{ render(controller('SyliusUserBundle:Security:login'))}}
And in Security.yml
sylius_shop_login:
path: /login
methods: [GET]
defaults:
_controller: sylius.controller.security:loginAction
_sylius:
template: "#SyliusShop/login.html.twig"
logged_in_route: sylius_shop_account_dashboard
How to include template in twig controller render?
Edit:
Any other better approach to show login form in popup? My plan is to include custom_login.html.twig in Layout twig using render(controller('SyliusUserBundle:Security:login')). But not able to send specific twig templete to controller.
In the Symfony2 project, I'm working on, the translations are files in multiple domain files like
foo.en_GB.xlf
bar.en_GB.xlf
buz.en_GB.xlf
...
foo.de_DE.xlf
bar.de_DE.xlf
buz.de_DE.xlf
...
foo.fr_FR.xlf
...
So in the Twig files I have to define the domain, e.g.:
{% trans from 'my_domain' %}my_key{% endtrans %}
Actually I don't need the domains in this project. All the translations are part of one big domain. So, I want (1) to use multiple translation files and (2) in the same time not to care about the domain, so that
{% trans %}my_key{% endtrans %}
should work for the my_key translated in any /.../translations/*.xlf file.
How to use multiple translation files without domains in a Twig template in Symfony2?
This can be achieved by creating a custom loader without break nothing, all you need is to use a different file extension:
namespace AppBundle\Translation\Loader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
class FooFileLoader extends XliffFileLoader
{
/**
* {#inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
// ignoring $domain var and pass 'messages' instead
return parent::load($resource, $locale, 'messages');
}
}
Register the translation loader:
services:
app.translation.loader.foo:
class: AppBundle\Translation\Loader\FooFileLoader
tags:
- { name: 'translation.loader', alias: 'foo' }
Later, you must name all your files to:
bar.en_GB.foo
baz.en_GB.foo
bar.de_DE.foo
baz.de_DE.foo
...
All your translation files with .foo extension will be merged and included into messages domain.
You can set the translation domain for an entire Twig template with a single tag:
{% trans_default_domain 'app' %}
I have added The following annotation routing to my controller.
/**
* #Route("/{_locale}/over-ons", name="_about-us_nl", defaults={"_locale": "nl"}, requirements={"_locale": "nl|en|de"})
* #Route("/{_locale}/about-us", name="_about-us_en", defaults={"_locale": "en"}, requirements={"_locale": "nl|en|de"})
* #Route("/{_locale}/uber-uns", name="_about-us_de", defaults={"_locale": "de"}, requirements={"_locale": "nl|en|de"})
*/
Now I want use the correct router in twig. But therefore I need to combine a string with an app.request. What is the syntax for this?
Following is not working:
{{ path( '_about-us_' ~ app.request.get('_locate') ) }}
I suggest you to use the JMSI18nRoutingBundle (check here the doc).
This bundle able you t o define your route as example:
routing.yml
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: prefix
Resulting URLs:
- /de/kontakt
- /en/contact
The bundle provide more knd of customization as described here
The Bundle provide the correct URL without specify the locale. You can speficy the locale for generating the url:
<!-- uses locale of the request context to generate the route -->
Contact
<!-- sometimes it's necessary to generate routes for a locale other than that
of the request context, then you can pass it explicitly -->
Deutsch
English
Hope this help
Use the following :
{{ path('_about-us_', { '_locale': app.request.locale }) }}
I'm adding a language switcher on my website.
my parameters are like this :
avc_coming_soon:
resource: "#AVCComingSoonBundle/Controller/"
type: annotation
prefix: /{_locale}
defaults:
_locale: en
requirements:
_locale: |fr
en is set by default
In my twig, I did that:
<div class="languages">
<ul class="lang-menu">
<li class="en"><img src="{{ asset('images/flag_en.gif') }}" alt="EN"></li>
<li class="fr"><img src="{{ asset('images/flag_fr.gif') }}" alt="FR"></li>
</ul>
</div>
But when I click on 'English', my path become www.mysite.com/en or the good route is www.mysite.com/ (without the /en) because in parameters, I have this :
defaults:
_locale: en
How to get the default _locale in twig ?
{{ path(app.request.get('_route'), {'_locale': <<<default>>> }) }}
thank you :)
You need to use app.request.attributes:
{{ path(app.request.get('_route'), {'_locale': app.request.attributes.get('_locale') }) }}
If you have configuration in controller you need to set default value in function parameter:
/**
* #Route("/", name="coming_soon", options={"expose"=true}, requirements={"_locale" = "fr|en"})
*/
public function indexAction($_locale = 'en')
{
...
}
I use this bundle: https://github.com/schmittjoh/JMSI18nRoutingBundle
Thanks to the bundle I never have to mess with locales..
You can configure the bundle to prefix everything except the default locale (which is what you wan't, reading the comments to the other answer).
And for the language switcher you simply set the _locale on any given route.
when using '%locale%' parameter from service.yaml the value in twig file does not change using subscriber. Using this code works for me:
app.request.session.get('_locale')
I'm using Symfony , I wanna redirect the user to a specific frame in a page
So How can i translate this <a href="index.html#about"/> to twig ? I tried <a href="{{path('pl_index')}}#about"/> but it doesnt work
in twig you can use {{path('_welcome') }} and it will send you to your home index, check your route.yml and you can see what are the paths generated by symfony.
for example this is my config.yml:
//savabundle config.yml
sava_inventario_construccion:
path: /productos/construccion
defaults: { _controller: savaInventarioBundle:Inventario:construccion }
sava_inventario_index:
path: /productos/
defaults: { _controller: savaInventarioBundle:Inventario:index }
if i want to generate www.mypage.com/productos/construccion#about link, this is what my html should look like
<a href="{{path('sava_inventario_construccion') }}#about"/>
you can read more in here
http://symfony.com/doc/current/book/templating.html