1

how is it possible to match url like www.mydomain.com/en/aboutus to

controller -> index
action -> aboutus
lang -> en

in zf2 module routing config ?

in zf1 we fix that by something like this

$route = new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);

but the aproeach is something else we want first determine what the language is in url then look into what controller or action user is requesting

4

2 回答 2

2

zf2 has hirachy support in routers so you can build your routes like a tree

for your situation you have to create a parent route that match lang in url for example

www.mydomain.com/en or www.mydomain.com/fa or www.mydomain.com/de or ....

then in it children write route for others

for code example :

'langroute' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/[:lang]',
                    'defaults' => array(
                        'lang' => 'en',
                    ),
                    'constraints' => array(
                        'lang' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'home' => array(
                        'type' => 'Zend\Mvc\Router\Http\Literal',
                        'options' => array(
                            'route' => '/',
                            'defaults' => array(
                                'controller' => 'Application\Controller\Index',
                                'action' => 'index',
                            ),
                        ),
                    ),
                    // The following is a route to simplify getting started creating
                    // new controllers and actions without needing to create a new
                    // module. Simply drop new controllers in, and you can access them
                    // using the path /application/:controller/:action
                    'aboutus' => array(
                        'type' => 'Zend\Mvc\Router\Http\Literal',
                        'options' => array(
                            'route' => '/aboutus',
                            'defaults' => array(
                                'controller' => 'Application\Controller\Index',
                                'action' => 'aboutus',
                            ),
                        ),
                    ),
),

as you can see the langrout match the en de fa or ... lang text then the childern route check for inner page in this example the url www.mydomain.com/en/ match the lang en and the route home

于 2012-12-07T18:42:33.550 回答
1

请将此代码添加到您的模块module.config.php

  return array(
    'router' => array(
        'routes' => array(
               // The following is a route to simplify getting started creating
                // new controllers and actions without needing to create a new
                // module. Simply drop new controllers in, and you can access them
                // using the path /Module_Name/:controller/:lang/:action
                'your_route_name_here' => array(
                    'type'    => 'Literal', 
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Module_Name\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',

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

             ),
         ),
     ); 
于 2013-05-11T07:50:10.970 回答