Symfony Twig - get default _locale in twig - symfony

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

Related

Symfony2 twig path - wrong generated URL behind

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

Symfony & Twig translate routing url by combining string with app.request

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

Symfony2, how to pass a parameter off route

Id like to know how to pass a parameter off route
route:
frontend_agences_list:
path: /agences/{page}
defaults: { _controller: ProjectFrontendBundle:Frontend:listAgences, page: 1 }
Is it like this ?
twig:
<a href="{{ path('frontend_agences_list') }}?mode=list">
<a href="{{ path('frontend_agences_list') }}?mode=grid">
<a href="{{ path('frontend_agences_list') }}?mode=block">
in fact, I'd like to display results in 3 modes, list , grid and block. In the controller I do a test ,If mode=list so render "list-view.html.twig", elseif mode=grid so render "grid-view.html.twig" ....
Is it the good way to do this or there is another way to do it ?
Twig:
list mode
grid mode
block mode
Your controller:
public function listAgencesAction()
{
$mode = $this->get('request')->get('mode');
if($mode == 'list') {
...
}
}

redirect to page#about using symfony&twig

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

FOSUserBundle : no route found for Security:Login

I post a problem here few days ago :
FOSUserBundle: embedding the login form and choosing its template
But it's pretty complicated, and I think behind this problem, there is maybe a simple problem of route, so I think it's a good idea to create a parallel topic, delete all the modifications I do, and start with fresh new installation.
So, I install FOSUserBundle, I have a WelcomeBundle, which contains the several pages of my website, and I also create a fresh UserBundle, which only contains the User Entity for FOSUserBundle, nothing else (no override of anything).
In my rsWelcomeBundle, in my index.html.twig, I put this simple code :
{% extends "rsWelcomeBundle::layout.html.twig" %}
{% block title "Page d'accueil" %}
{% block body %}
<div class="span6">
<div class="well">
<h2>Présentation du jeu</h2>
Je m'inscris !</p>
</div>
</div>
<div class="span6">
<div class="well">
{% render "FOSUserBundle:Security:login" %}
</div>
</div>
{% endblock %}
And I always have this error :
An exception has been thrown during the rendering of a template ("No route found for "GET Security:login"") in rsWelcomeBundle:Homepage:index.html.twig at line 1.
If I put this line for the render part it's working.
{% render(controller("FOSUserBundle:Security:login")) %}
But why ??
In the documentation I never see we've to use "render(controller".
Why I can't use directly : render "FOSUserBundle:Security:login" ?
In app/config/routing.yml I have :
tuto_welcome:
resource: "#rsWelcomeBundle/Resources/config/routing.yml"
prefix: /
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "#FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "#FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "#FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
In the UserBundle I don't have routing.yml.
In the WelcomeBundle I have this in routing.yml :
rsWelcomeBundle_homepage:
pattern: /
defaults: { _controller: "rsWelcomeBundle:Homepage:index" }
It's been two days I try to fix this problem, could you help me ?
Thanks a lot to everyone !
{% render "FOSUserBundle:Security:login" %} is the Symfony 2.0 way to render a subrequest. {{ render(controller("FOSUserBundle:Security:login")) }} is the syntax for 2.1+. Both syntax basically mean the same exact thing.
Documentation about subrequests is here: http://symfony.com/doc/current/quick_tour/the_view.html#embedding-other-controllers
Try using {% render url('your url') %} instead.

Resources