Zend Framework 2将子域路由到模块 [英] Zend Framework 2 Routing subdomains to module

查看:100
本文介绍了Zend Framework 2将子域路由到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索很长时间后没有成功. 在我放弃之前,我想问:

After searching a long time with no success. before I give up, I would like to ask:

是否可以将子域路由到 Zend Framework 2 中的模块?像:

Is there a way to route a subdomain to a module in Zend Framework 2? like:

子域 => 模块
api.site.com => api
dev.site.com => dev
admin.site.com =>管理员
site.com =>公共
...

Subdomain => Module
api.site.com => api
dev.site.com => dev
admin.site.com => admin
site.com => public
...

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

I tried doing it like this but I can't get access to controllers other than the default (Index).

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

谢谢您抽出宝贵的时间来帮助我.

Thank you for taking the time to help me.

推荐答案

Zend Framework 2没有路由到模块的概念.所有路由映射都在URI模式(用于HTTP路由)和特定的控制器类之间.也就是说,Zend\Mvc提供了一个事件监听器( ),您可以定义一个URI模式,该模式根据给定的模式映射到多个控制器,从而模拟模块路由".要定义这样的路由,您可以将其作为路由配置:

Zend Framework 2 doesn't have a notion of routing to modules; all routing mappings are between a URI pattern (for HTTP routes) and a specific controller class. That said, Zend\Mvc provides an event listener (Zend\Mvc\ModuleRouteListener) which allows you to define a URI pattern that maps to multiple controllers based on a given pattern, and so emulates "module routing". To define such a route, you would place this as your routing configuration:

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

(点击此处查看@ ZendSkeletonApplication的示例)

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

This is only half of the equation, though. You must also register every controller class in your module using a specific naming format. This is also done through the same configuration file:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),

数组键是ModuleRouteListener用来查找正确控制器的别名,并且必须采用以下格式:

The array key is the alias ModuleRouteListener will use to find the right controller, and it must be in the following format:

<Namespace>\<Controller>\<Action>

分配给此数组键的值是控制器类的标准名称.

The value assigned to this array key is the fully-qualified name of the controller class.

(点击此处查看@ ZendSkeletonApplication的示例)

注意:如果您不使用ZendSkeletonApplication,或者已删除它的默认Application模块,则需要在您自己的模块之一中注册ModuleRouteListener. 单击此处以查看ZendSkeletonApplication如何注册此侦听器的示例

NOTE: IF you aren't using ZendSkeletonApplication, or have removed it's default Application module, you will need to register the ModuleRouteListener in one of your own modules. Click here to see an example of how ZendSkeletonApplication registers this listener

这篇关于Zend Framework 2将子域路由到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆