Redirecting from Symfony routing

0

I have a routing in my Symfony project that points to different controllers, in different routes, like this:

# GLOBAL DE LA APLICACIÓN
entry_point:
  path: /
  defaults: { _controller: AppBundle:Default:index}
  options:
    expose: true

# PARTE ADMIN
admin_index:
  path: /admin
  defaults: { _controller: AppBundle:Admin\Admin:index}

admin_sec:
  path: /admin
  defaults: { _controller: AppBundle:Admin\Admin:sec}

#PARTE PUBLICA
user_index:
  path: /user
  defaults: { _controller: AppBundle:User\User:index}
# GLOBAL DE LA APLICACIÓN
entry_point:
  path: /
  defaults: { _controller: AppBundle:Default:index}
  options:
    expose: true

# PARTE ADMIN
admin_index:
  path: /admin
  defaults: { _controller: AppBundle:Admin\Admin:index}

admin_sec:
  path: /admin
  defaults: { _controller: AppBundle:Admin\Admin:sec}

#PARTE PUBLICA
user_index:
  path: /user
  defaults: { _controller: AppBundle:User\User:index}

In order to use i18n I have put the following in the app / config routing:

Redireccion_por_defecto:
    path: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /es
        permanent: true

router:
    resource: '@AppBundle/Resources/config/routing.yml'
    prefix: /{_locale}
    requirements:
      _locale: es|en|fr|it|pt|de|ca

It is about that, when a route is called, the redirector adds the prefix _locale that, as you see, I have set it as a requirement.

In config.yml I have enabled the translator, like this:

parameters:
    locale: es
    default_locale: es

framework:
    #esi: ~
    translator:
      fallbacks: ['%locale%']
      enabled: true

When I type localhost: 8000 , everything is fine. Automatically "recreates" the route as localhost: 8000 / es , and it goes to the corresponding controller and shows the view. However, if I type localhost: 8000 / user , it does not rebuild it to localhost: 8000 / es / user , but it looks for localhost: 8000 / user and, as the prefix locale is set to requirement (as seen in the code), throws a path exception not found. If I type localhost: 8000 / is / user by hand, if it shows the user page.

How can I make the redirection work for all routes, or for a group of routes? I'm using Symfony 3.3.

Thank you.

    
asked by Chefito 15.03.2018 в 16:32
source

1 answer

1

You have to add a default on the route, I guess it would look like this:

router:
    resource: '@AppBundle/Resources/config/routing.yml'
    prefix: /{_locale}
    defaults:   { _locale: es }
    requirements:
      _locale: es|en|fr|it|pt|de|ca
    
answered by 17.04.2018 / 12:57
source