FriendsOfSymfony / FOSJsRoutingBundle

A pretty nice way to expose your Symfony routing to client applications.
1.48k stars 261 forks source link

How to inject _locale in symfony 5 #413

Closed MaruanBO closed 3 years ago

MaruanBO commented 3 years ago

Hi friends!,

The requested ajax url it's not injecting _locale in GET param url. In post it's working very well.. im using the LocaleSubsriber prodived by symfony (https://symfony.com/doc/current/session/locale_sticky_session.html#creating-a-localesubscriber)

If i inject manually _locale, works.. but automatically not

Request without ajax, eg:

Request Attributes

Key Value

"App\Controller\Web\PriceListController::index" "security.firewall.map.context.users" [▼ Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted {#1285 ▶} ] "es" "client_price_list_index" [▼ "schemes" => [▶] "_locale" => "es" "id" => "10" ]

Request from ajax eg:

Request Attributes

Key Value

"App\Controller\Web\PriceListController::ajaxIndex" "security.firewall.map.context.users" [▼ Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted {#1285 ▶} ] "en" "ajax_price_tier" [▼ "schemes" => [▶] "_locale" => "en" ] [▼ "https" ]


`marobou@pc335:/var/www/doctorsender-smart$ bin/console fos:js-routing:debug

+-----------------------------------+----------+--------+------+-------------------------------------------------+ | Name | Method | Scheme | Host | Path | +-----------------------------------+----------+--------+------+-------------------------------------------------+ | ajax_client_index | GET | ANY | ANY | /{_locale}/client/ | | ajax_client_users | GET | ANY | ANY | /{_locale}/client/{id}/users | +-----------------------------------+----------+--------+------+-------------------------------------------------+ `

Also, i have the last update of FOSJSRoutingBundle,

MaruanBO commented 3 years ago

Fixed, adding in base.html.twig the next code:

<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
<script>
        // sobre escribimos la función original
        Routing.generateImpl = Routing.generate;
        // sobre escribimos la función original agregando nuestro _locale
        Routing.generate = function (url, params) {
            var paramsExt = {};
            if (params) {
                paramsExt = params;
            }
            if (!paramsExt._locale){
                paramsExt._locale = '{{ app.request.locale }}';
            }
            return Routing.generateImpl(url, paramsExt);
        }
</script>
<script>var locale = "{% if app.request.locale is defined %}{{ app.request.locale|split('_')[0] }}{% else %}en{% endif %}</script>

Explain

In my case i need to override generate function by adding a default _locale from request locale. To make it u need to change name of initial method (Routing.generateImpl). This will make the _locale available in all JS files.

Usage

In any JS:

Routing.generate('path to route');