dt3674 2015-06-30 09:53
浏览 23

ZF2 Routing by Hostname与其他模块一起使用

I added a reseller subdomain on my myhost.com (reseller.myhost.com) and I use it for routing to my Reseller module. Read this question I posted before here: click here

My Reseller route config looks this:

'router' => array(
    'routes' => array(
        'Reseller' => array(
            'type'    => 'Hostname',
            'options' => array(
                'route'    => 'reseller.myhost.com',
                'constraints' => array(

                ),
                'defaults' => array(
                    'controller' => 'Reseller\Controller\Reseller',
                    'action'     => 'index'
                )
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'home' => array(
                    'type' => 'Zend\Mvc\Router\Http\Literal',
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Reseller\Controller',
                            'controller'    => 'Reseller',
                            'action'        => 'index',
                         ),
                    ),
                ),

            )
        )
    )
)

My createdAd route matches on reseller.myhost.com/createdAd but I expect routes from other modules not work on this reseller subdomain.

and here is my advertise route configuration

    'router' => array(
         'routes' => array(
             'locate' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/locate[/:cityName][/:CityId][/:CategoryId][/:categoryName]',
                     'constraints' => array(

                     ),
                     'defaults' => array(
                         'controller' => 'Advertise\Controller\Advertise',
                         'action'     => 'index',
                     ),
                 ),
             ),


             'createAd' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/createAd[/:subCategoryId]',
                     'constraints' => array(

                     ),
                     'defaults' => array(
                         'controller' => 'Advertise\Controller\Advertise',
                         'action'     => 'createAd',
                     ),
                 ),
             ),




         ),
     ),


 ));

be notice that i want to advertise module work without subdomain and work normally and only reseller module work with subdomain

Why does this occur?

  • 写回答

2条回答 默认 最新

  • dqluw20882 2015-07-01 07:28
    关注

    I understand from your question: you expect the createAd route not to work on the subdomain. So reseller.myhost.com/createdAd should not match instead you want a to match on the route without subdomain myhost.com/createdAd.

    I would suggest that you should create a separate route definition for the Advertise module.

    Your route config in Advertise module (module/Advertise/config/module.config.php)

    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Advertise\Controller\Advertise',
                        'action'     => 'index'
                    )
                ),
            )
            'createAd' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/createAd',
                    'defaults' => array(
                        'controller' => 'Advertise\Controller\Advertise',
                        'action'     => 'createAd',
                    )
                )
            )
        )
    )
    

    Your route config in Reseller module (module/Reseller/config/module.config.php)

    'router' => array(
        'routes' => array(
            'Reseller' => array(
                'type'    => 'Hostname',
                'options' => array(
                    'route'    => ':reseller.myhost.com',
                ),
                'may_terminate' => false,
                'child_routes' => array(
                    'home' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route'    => '/',
                            'defaults' => array(
                                'controller' => 'Reseller\Controller\Reseller',
                                'action'     => 'index'
                            )
                        )
                    )
                )
            )
        )
    ),
    

    You can distinguish matches because of the subdomain.

    The routes home and createAdd match the Advertise module without subdomain.

    The route reseller.home matches the index in Reseller module within subdomain reseller.myhost.com.

    Check for more details also the Hostname routing example here in the ZF2 documentation

    评论

报告相同问题?