简体   繁体   中英

ZF2 - Language Routes

I need to create a router configuration in Zend Framework 2 like:

http://www.example.com/en
http://www.example.com/fr
http://www.example.com/es

Then the route of the modules like my "cms" module must match:

http://www.example.com/en/cms/test.html (english version)
http://www.example.com/fr/cms/test.html (french version)
http://www.example.com/es/cms/test.html (spanish version)

of course this link must match the english default language:

http://www.example.com/cms/test.html (english version)

I have already used the SimLocale without success because it doesn't handle the asset links as well. So I prefer to handle the language selection manually by a simple parameter for all my custom modules.

The question has been solved on #zftalk IRC today (thanks Michelangelo to contribute back based one what we explained to you...).

To solve the problem, we had to have a look at the defined routes:

'router' => array(
    'routes' => array(
        'language' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '[/:lang]',
                'defaults' => array(
                    '__NAMESPACE__' => 'Cms\Controller',
                    'controller' => 'Index',
                    'action' => 'page',
                    'lang' => 'en',
                    'slug' => 'homepage'
                ),
                'constraints' => array(
                    'lang' => '[a-z]{2}'
                )
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'list' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/cms',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Cms\Controller',
                            'controller' => 'Index',
                            'action' => 'index',
                            'page' => 1
                        ),
                    ),
                ),
                'page' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/:slug].html',
                        'constraints' => array(
                            'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'page',
                        ),
                    ),
                ),
                'search' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/search/[query/:query]',
                        'constraints' => array(
                            'query' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'search',
                            'query' => null,
                        ),
                    ),
                ),
                'paginator' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/list/[page/:page]',
                        'constraints' => array(
                            'page' => '[0-9]*',
                        ),
                        'defaults' => array(
                            'page' => 1,
                        ),
                    ),
                ),
            ),
        ),

Then, the updated description of the problem was the following:

mikunos: http://www.example.com/en (homepage) this is ok

mikunos: http://www.example.com/en/cms (cms list) this is ok

mikunos: http://www.example.com/en/cms/about.html (page) this not

Which have led us on changing the 'page' route as a child route of 'list' (/cms).

Then the discussion finished with indications on what to do to setup the default language if none have been selected:

mikunos: as you have seen I need two main routes

mikunos: the first one is /lang/module and the second one is /module

mikunos: so I have to create two branches

mikunos: right?

tdutrion: you may want to use an url rewriting or something here

tdutrion: I would go for a redirect 301 from /module to /en/module so you have no duplicate content

tdutrion: and then no route can be accessed without a language

mikunos: ok

tdutrion: you can do it different ways, but I think the best one would be creating a event listener and do that before routing

tdutrion: you can also do it in your .htaccess of you are using apache, but then what if you migrate to IIS or Ngnix or else...

mikunos: interesting

tdutrion: again I believe Jurian Sluiman's code does that

You can use subdomains for each language you need witch I suppose you don't want or you really don't need (eg. /en) to change the language of your application and/or web site. Is mandatory to use /en, /fr, etc.?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM