Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Framework 2将子域路由到模块_Php_Zend Framework2 - Fatal编程技术网

Php Zend Framework 2将子域路由到模块

Php Zend Framework 2将子域路由到模块,php,zend-framework2,Php,Zend Framework2,在寻找了很长时间后没有成功。 在我放弃之前,我想问: 是否有办法将子域路由到Zend Framework 2中的模块?比如: 子域=>模块 api.site.com=>api dev.site.com=>dev admin.site.com=>admin site.com=>public … 我试着这样做,但除了默认的(索引)之外,我无法访问其他控制器 感谢您抽出时间来帮助我。如果我正确理解的幻灯片#39,就每个模块而言,定义您的子域主机非常简单,即: 'router' => array(

在寻找了很长时间后没有成功。 在我放弃之前,我想问:

是否有办法将子域路由到Zend Framework 2中的模块?比如:

子域=>模块
api.site.com=>api
dev.site.com=>dev
admin.site.com=>admin
site.com=>public

我试着这样做,但除了默认的(索引)之外,我无法访问其他控制器

感谢您抽出时间来帮助我。

如果我正确理解的幻灯片#39,就每个模块而言,定义您的子域主机非常简单,即:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Hostname',
            'options' => array(
                'route' => 'api.site.com',
                'defaults' => array(
                    '__NAMESPACE__' => 'Api\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            )
        )
    ),
),

诸如此类,每个模块都要自己完成

Zend Framework 2没有路由到模块的概念;所有路由映射都在URI模式(用于HTTP路由)和特定控制器类之间。也就是说,
Zend\Mvc
提供了一个事件侦听器(),它允许您定义一个URI模式,该模式根据给定的模式映射到多个控制器,从而模拟“模块路由”。要定义此类路由,请将其作为路由配置:

'router' => array(
    'routes' => array(
         // This defines the hostname route which forms the base
         // of each "child" route
        'home' => array(
            'type' => 'Hostname',
            'options' => array(
                'route' => 'site.com',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This Segment route captures the requested controller
                // and action from the URI and, through ModuleRouteListener,
                // selects the correct controller class to use
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Index',
                            'action'     => 'index',
                        ),
                    ),
                ),
            ),
        ),
    ),
),
()

不过,这只是等式的一半。您还必须使用特定的命名格式注册模块中的每个控制器类。这也可以通过相同的配置文件完成:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
数组键是alias ModuleRootListener用于查找正确控制器的别名,必须采用以下格式:

<Namespace>\<Controller>\<Action>
\\)


注意:如果您没有使用Zendskletonapplication,或者已经删除了它的默认应用程序模块,则需要在您自己的一个模块中注册ModuleRootListener

非常感谢你的解释清楚而准确。我接受了你的解决方案。我在这方面做了很多努力。我使用mamp pro,在我的虚拟主机名上创建了一个子域,所以在我使用reseller.myhost.com/test之后,它是正常的,但如果我写reseller.myhost.com,它将进入应用程序的索引,而不是我的虚拟主机的索引module@Sapher但为什么其他模块的其他路由在当前子域上还起作用呢?如果它看起来很愚蠢,那么很抱歉,但你认为呢必须为api子域注册新的Web服务器虚拟主机吗?
<Namespace>\<Controller>\<Action>