简体   繁体   English

ZF2按主机名路由与其他模块一起使用

[英]ZF2 Routing by Hostname works with other modules

I added a reseller subdomain on my myhost.com ( reseller.myhost.com ) and I use it for routing to my Reseller module. 我在myhost.comreseller.myhost.com )上添加了一个reseller子域,并将其用于路由到我的Reseller模块。 Read this question I posted before here: click here 在这里阅读我之前发布的问题: 单击此处

My Reseller route config looks this: 我的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',
                         ),
                    ),
                ),

            )
        )
    )
)

My createdAd route matches on reseller.myhost.com/createdAd but I expect routes from other modules not work on this reseller subdomain. 我的createdAd路由在reseller.myhost.com/createdAd上匹配,但我希望其他模块的路由在此reseller子域上不起作用。

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? 为什么会发生这种情况?

I understand from your question: you expect the createAd route not to work on the subdomain. 我从您的问题中了解:您希望createAd路由在该子域上不起作用。 So reseller.myhost.com/createdAd should not match instead you want a to match on the route without subdomain myhost.com/createdAd . 因此, reseller.myhost.com/createdAd myhost.com/createdAd不应该匹配,而是要在没有子域myhost.com/createdAd的路由上进行匹配。

I would suggest that you should create a separate route definition for the Advertise module. 我建议您为Advertise模块创建一个单独的路由定义。

Your route config in Advertise module ( module/Advertise/config/module.config.php ) 您在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',
                )
            )
        )
    )
)

Your route config in Reseller module ( module/Reseller/config/module.config.php ) 您在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'
                        )
                    )
                )
            )
        )
    )
),

You can distinguish matches because of the subdomain. 您可以通过子域来区分匹配项。

The routes home and createAdd match the Advertise module without subdomain. 该路线homecreateAdd匹配的Advertise模块,而不子域。

The route reseller.home matches the index in Reseller module within subdomain reseller.myhost.com . 路由reseller.home与子域reseller.myhost.com中的Reseller模块中的索引匹配。

Check for more details also the Hostname routing example here in the ZF2 documentation 还要在ZF2文档中检查更多详细信息,以及主机名路由示例

You should have a "root" hostname for all your standard routes not on the subdomain route. 您应该为所有标准路由(不在子域路由上)使用一个“ root”主机名。 Eg: 例如:

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

Now you can add your 'createAd' route (and other routes) as a child route of the 'myhost' route. 现在,您可以将“ createAd”路由(和其他路由)添加为“ myhost”路由的子路由。 Eg: 例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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