4

I would like to configure the Symfony locale options so that I can successfully access the following routes:

/route
/{locale}/route

Currently, I can access /{locale}/route and I get my view, however, /route returns a 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 include the locale, it all works. If I exclude the locale, I get the No route found error.

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
  • 1
    I do not think the following pattern is applicable. The `/route` and `/{_locale}/route` are two different routes which are matched based on the requirements of the `_locale` (amongst other) which are defined as `en` or `fr`. When you define the default `_locale` it will only help you when generating routes and omitting the `_locale` parameter. If you really want to do though (and I don't advise it), you may write a custom `RouteMatcher`. – Boris Guéry Jan 18 '17 at 14:04

2 Answers2

5

You have to define another route to cover the scenario without the provided locale, try changing your 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()
    {
        ...
    }
}
lordrhodos
  • 2,689
  • 1
  • 24
  • 37
  • Turns out you cannot apply multiple routes at the `Controller` class level, however, I can apply this to the `Action` and it works as desired. – Peppermintology Jan 23 '17 at 11:35
0

I agree with what Boris commented up there.

An alternative solution about making a RouteMatcher is to use EventListener and redirect user into same route with the default Locale (Check the answer that has more upvotes)

In my opinion doing this with EventListeners like Athlan explained there is the way to go. I would not mess with making your own RouteMatcher. Just see what lordrhodos wrote, never tried to put two routes like this in a Controller annotations, but I think if you have only one Controller is the way to go. If they are going to be a lot might be a lot cleaner to do this with EventListeners in only one place.

Community
  • 1
  • 1
Martin Fasani
  • 825
  • 7
  • 20