ZF2 按主机名路由可与其他模块配合使用


ZF2 Routing by Hostname works with other modules

我在myhost.com(reseller.myhost.com(上添加了一个reseller子域,并使用它来路由到我的Reseller模块。 阅读我之前在这里发布的这个问题:点击这里

我的Reseller路由配置如下所示:

'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',
                         ),
                    ),
                ),
            )
        )
    )
)

我的createdAd路由与 reseller.myhost.com/createdAd 匹配,但我希望来自其他模块的路由不适用于此reseller子域。

这是我的广告路由配置

    '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',
                     ),
                 ),
             ),


         ),
     ),

 ));

请注意,我想宣传没有子域的模块工作并且正常工作,只有经销商模块可以使用子域

为什么会这样?

我从您的问题中了解到:您希望createAd路由在子域上不起作用。所以reseller.myhost.com/createdAd不应该匹配,而是你想要一个在没有子域myhost.com/createdAd的路由上匹配。

我建议您为 Advertise 模块创建一个单独的路由定义。

您在Advertise模块中的路由配置 ( 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',
                )
            )
        )
    )
)

您在Reseller模块中的路由配置 ( 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'
                        )
                    )
                )
            )
        )
    )
),

您可以区分子域中的匹配项。

路由homecreateAdd匹配没有子域的Advertise模块。

路由reseller.home与子域reseller.myhost.com Reseller模块中的索引匹配。

请查看更多详细信息,

以及 ZF2 文档中的主机名路由示例

对于不在子域路由上的所有标准路由,您应该有一个"root"主机名。例如:

'router' => array(
    'routes' => array(
        'myhost' => array(
            'type'    => 'Hostname',
            'options' => array(
                'route'    => 'myhost.com',
            ),
        ),
    ),
),

现在,您可以将"createAd"路由(以及其他路由(添加为"myhost"路由的子路由。例如:

'router' => [
    'routes' => [
        'myhost' => [
            'child_routes' => [
                'createAd' => array(
                     'type'    => 'segment',
                     'options' => array(
                         'route'    => '/createAd[/:subCategoryId]',
                         'constraints' => array(
                         ),
                         'defaults' => array(
                             'controller' => 'Advertise'Controller'Advertise',
                             'action'     => 'createAd',
                         ),
                     ),
                 ),
             ],
         ],
     ],
],