I have implemented the locale in my symfony2 project. Now the problem is if i manually change the locale in the url to something other than locale values it is just taking that value. How do i restrict this?
For example: http://myproject.com/en changing to http://myproject.com/asdf .
Now asdf is not a locale value in my config! but still it displays in my locale switcher as Country-asdf!!
How do i restrict url editing of locale?
Thanks.
You can define the route restrictions in your routing . If it is YML ,
homepage:
pattern: /{locale}
defaults: { _controller: AcmeDemoBundle:Main:homepage, locale: en }
requirements:
culture: en|fr|in
Related
I'd like to prefix all my urls by an optional _locale. BTW, I'm handling fr and en.
The point is, when _locale is given I'll use it thanks to symfony Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest, if not I'll use the locale saved in the database thanks to my own listener (priority: 15) which runs immediately after the previous one.
What I've tried so far is the following:
app/config/routing.yml
entrypoint:
resource: 'locale_prefixed_routing.yml'
prefix: /{_locale}
And locale_prefixed_routing.yml imports bundles routings.
/fr/url/example works but /url/example does not and returns 404 page.
How do I tell symfony to use the locale in the url, if does not exist use the one in the database?
I suppose your routes are defined like this in your locale_prefixed_routing.yml :
my_route:
path: /
defaults:
Actually routes like /url/example without any locale are not defined in your routing because you ask to prefix all the routes loaded in locale_prefixed_routing.yml by the locale. So it is normal to get an error when requesting such a route without locale.
You can try to make another routing file named non_localized_routing.yml containing the same routes except that you will use specific defaults.
Example :
route_example:
path: /url/example
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/url/example
permanent: true
Tell me if it permits you to get rid of your problem, I haven't test it yet.
For me it sounds weird to store the locale in the database when the locale is not provided by the user. You can simply add a default one:
framework:
default_locale: en
I am currently stuck on the multi-site management in Symfony2.
I have a Symfony2 project that allows multiple sites. Everything works fine but I block on managing files "routing".
My /app/config/routing.yml file I wish I could load the correct file routing.yml compared to the domain name:
project_routing:
resource: "%MY_DOMAIN%/routing.yml"
Unfortunately I can not put variable %my_domain% a file routing so I do not see how to fix this ...
The structure:
app/config/routing.yml is redirected to my %my_domain%
app/config/www.mywebsite.com/routing.yml
app/config/www.otherwebsite.com/routing.yml
So 1) base locale you can specify in app/parameters.yml
like this:
parameters:
locale: en
where locale: en default locale.
2) In default app/routing.yml file you should specify link for routing in your bundle like this:
fob_rental:
resource: "#fobRentalBundle/Resources/config/routing.yml"
prefix: /
3) In my src/fob/RentalBundle/Resources/config/routing.yml route looks like:
fob_offer_new:
pattern: /{_locale}/offers/new
defaults: { _controller: fobRentalBundle:Offer:new }
requirements:
_locale: en|spa
I'm trying to use a _locale variable in a routing, and if is not set use the default locale. The routing looks like this:
#/app/config/routing.yml
xxx_yyy_zzz:
resource: "#XYZBundle/Resources/config/routing.yml"
prefix: /orientation/{_locale}
defaults: { _locale: %locale% }
What I'm trying to do is to be able to use "domain.com/orientation/WhateverLanguage" or "domain.com/orientation", but it doesn't work.
When I type "domain.com/orientation" I get a "No route found for "GET /orientation".
Am I missing something? Can this be done?
Thanks !
Maybe this cookbook from the Symfony doc will help you.
http://symfony.com/doc/master/cookbook/routing/service_container_parameters.html
Grtz
How can I set default controller and action in symfony2. Where to configure this settings.
Which annotation do you use?
My default Route is in the file app/config/routing.yml
_index:
pattern: /
defaults: { _controller: ACMEUserBundle:Default:index }
If you have installed with DemoBundle there is a Default Route in the file src/Acme/DemoBundle/Resources/config/routing.yml.
Look the Link it's show some idea to you
http://symfony.com/doc/current/book/routing.html
cant run the controller with symfony 2
Some time ago, I started translating the Symfony 2.1 website I'm working on, using the "correct way" through /{ _locale } in the URL. Now the translation worked fine for most routes, but some of them (/login, /register, ...) kept coming in the default language (fr in this instance), because they didn't have the { _locale } part in their url.
Through the debug panel I realised that the locale was not actually changed through url navigation (unlike this had me believe).
So I then went there, where I found information and link to set up a listener in order to change the session locale in function of the path that is called.
All defaults locale declarations (both in config.yml and the listener itself) are "fr".
When I switch languages, I can actually see on the debug panel that the session locale changes. However, it would appear that the translation has somehow been broken : every translation bit is made in english, even though the default language is french, and despite the session locale being "fr".
Where could that come from? How can I fix it?
Thanks in advance.
More technical information:
routing.yml:
AuraeLCUserBundle:
resource: "#AuraeLCUserBundle/Resources/config/routing.yml"
prefix: /{_locale}/
defaults: { _locale: fr }
requirements:
_locale: en|fr
a twig example:
<p>{{ 'layout.greeting.welcome'|trans({}, 'AuraeLCUserBundle') }}</p>
3 things here are critical.
default lang (setted in config) and if your using translatable doctrine extension:
# app/config/config.yml
framework:
session: { default_locale: 'fr_FR' }
translator: { fallback: 'en' }
.....
stof_doctrine_extensions:
default_locale: 'fr_FR'
They must be different because of circular reference exception appears when the default locale won't go.
Translator is looking for files yaml|xml|gettext with sufix fr_FR ex:
messages.fr_FR.yml
messages.en.yml
yourcatalogue.fr_FR.yml
yourcatalogue.en.yml
Read this for more details:
Change default locale in Symfony2
http://symfony.com/doc/2.0/book/translation.html
http://0hlsson.se/2011/07/27/symfony2-and-translatable-example/