Question

Je crée une application multilingue en utilisant ZF2.et ne peut pas déterminer comment ajouter une URL partielle qui constituera la base de chaque URL quels que soient les modules.

http://localhost/en/us/application/index/index/

Je comprends parfaitement comment configurer /[:namespace[/:controller[/:action]]] en utilisant DI

http://localhost/application/index/index/
http://localhost/guestbook/index/index/
http://localhost/forum/index/index/

Ce que je ne comprends pas, c'est comment configurer un Partie itinéraire qui sera la base de tous les itinéraires.Dans ZF1, j'ai utilisé Chaînage d'itinéraires pour y parvenir..

Je dois donc configurer un Partie itinéraire de /[:lang[/:locale]] qui s'applique à l'ensemble du site, puis laissez le module configurer /[:namespace[/:controller[/:action]]] ou tout autre itinéraire nécessaire..

http://localhost/en/us/application/index/index/
http://localhost/zh/cn/application/index/index/
http://localhost/en/uk/forum/index/index/
Était-ce utile?

La solution

Je pense que ce que tu cherches c'est le child_routes clé de configuration.Jetez un oeil à comment ZfcUser configure son routage (ici):il crée une route littérale de base (/user), puis y enchaîne les sous-routes (/user/login, etc.) via le child_routes tableau.

Je pense que quelque chose comme ceci fera l'affaire pour vous :

'router' => array(
    'routes' => array(
        'myapp' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/[:lang[/:locale]]',
                'defaults' => array(
                    'lang'   => 'en',
                    'locale' => 'us',
                ),
            ),
            'may_terminate' => false,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'index',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
),

Ensuite, dans votre contrôleur, vous pouvez faire ceci pour obtenir la langue et les paramètres régionaux :

$this->params()->fromRoute('lang');
$this->params()->fromRoute('locale');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top