Symfony 5.1 route issue - symfony

I installed symfony 5.1.3 today and found the route configuration is something broken. I'm not sure if that is intended. But at least it's different from symfony 4.4. I have the follow setting in 4.4 and it works fine.
homepage:
path:
en-us: /
others: /{_locale}/
controller: App\Controller\IndexController::homepage
requirements:
_locale: en-us|zh-cn|zh-hk
The above settings allow me to access domain.com and other mydomain.com/en-us, mydomain.com/zh-cn and mydomain.com/zh-hk successfully.
But now in symfony 5.1, the above setting does not work now. I believe the _locale with special meaning to the system maybe the root cause. Can anyone give me a help?

I think you are using Localized Routes feature incorrectly.
Keys in path are locales. In your case, you have 2 locales: en-us and others. Try opening mydomain.com/others and you will see that it's working.
I suggest splitting this config into 2 routes:
homepage:
path: /
controller: App\Controller\IndexController::homepage
homepage_others:
path: /{_locale}/
controller: App\Controller\IndexController::homepage
requirements:
_locale: en-us|zh-cn|zh-hk

Related

Add optional _locale to routes

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

Dynamic routing file SF2

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

symfony/Routing - import yml resource twice

I'm trying to enable optional locale placeholder for all the routes without duplicating everything. My routes look like this:
site:
prefix: /
resource: "routes-site.yml"
site_i18n:
prefix: /{_locale}
resource: "routes-site.yml"
defaults: {_locale: pl}
requirements:
_locale: 'en'
But I get only site_i18n working. Why I cannot import same resource multiple times?
It's Symfony 2.2
Because the routes have same route name, the later import overrides routes from the first one.
Have a look at: BeSimpleI18nRoutingBundle. It allows you even localize the whole path, but in this case, you will just need localize prefix.

Symfony2 translation: locale is ignored

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/

How to display the 404 error page in Symfony2 dev environment

I want to work on the 404 page from the dev environment. I customize 404 using this file : app/Resources/TwigBundle/views/Exception/error.html.twig
This prod url work correctly : mysite.com/404
But this one mysite.com/app_dev.php/404 throw a NotFoundHttpException and give me the dev debug page.
Is it possible to display the error page instead of debug page ?
UPDATE:
The official documentation has now a chapter about that : Testing Error Pages during Development
To display error pages, in web/app_dev.php change second parameter to false
$kernel = new AppKernel('dev', false);
After testing what you need, change it back.
UPDATE
Thanks #user2019515 for pointing that out - now (2.3 and up) there's a link to WebfactoryExeptionsBundle in Symfony docs and the method I wrote above should not be used.
As of Symfony2.6 in dev environment, you can use the following route:
/_error/404.html
Where 404 is the error code to test and html the format of the request.
To be able to use this features, make sure you have the following entry in your routing_dev.yml file:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
You need to override the exception_full.html.twig template on development.
app/Resources/TwigBundle/views/Exception/exception_full.html.twig
Symfony2 uses this template to provide you with as much debugging information as possible during development.
When the kernel is in debug mode, Symfony2 will use exception_full.html.twig, otherwise it will use the specific templates you override.
See vendor/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php, specifically the showAction() and findTemplate() functions for more details.
Also you can add routes to your routing_dev.yml file
error404:
path: /404
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error404.html.twig
error500:
path: /500
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error500.html.twig

Resources