Symfony default locale without url

I would like to configure Symfony settings localeso that I can successfully access the following routes:

/route
/{locale}/route

Currently I can access /{locale}/route, and I am getting my view, however /routereturns No route found for "GET /route". My configuration is as follows:

#app/config/parameters.yml
parameters:
    locale: en

#app/config/config.yml
parameters:
    app_locales: en|fr
framework:
    translator: { fallback: "%locale%" }
    default_locale: "%locale%"

#app/config/routing.yml
app:
    resource: '@AppBundle/Controller/'
    type: annotation

My controller has the following annotations:

#src/AppBundle/Controller/Admin/MyController.php
/**
 *
 * @Route(
 *     "/{_locale}/admin/my",
 *     defaults={"_locale":"%locale%"},
 *     requirements={"_locale":"%app_locales%"}
 *     )
 */
class MyController extends Controller
{
    /**
     * @Route("/", name="admin_my_list")
     * @Method("GET")
     */
    public function listAction()
    {
        ...
    }
}

If I specifically turn it on locale, it all works. If I rule out locale, I get an error No route found.

+4
source share
2 answers

You must define a different route to cover the scenario without the locale provided, try changing the route definition to:

#src/AppBundle/Controller/Admin/MyController.php

class MyController extends Controller
{
    /**
     * @Route(
     *     "/admin/my",
     *     defaults={"_locale":"%locale%"},
     *     )
     * @Route(
     *     "/{_locale}/admin/my",
     *     requirements={"_locale":"%app_locales%"}
     *     )
     * @Method("GET")
     */
    public function listAction()
    {
        ...
    }
}
+3

, .

RouteMatcher EventListener ( , )

-, EventListeners, Athlan, , . RouteMatcher. , Lordrhodos, Controller, , , . , EventListeners .

0

Source: https://habr.com/ru/post/1667148/


All Articles