2013-05-06 113 views
0

我工作的一个项目ZF2 ZF2路由器的配置,我需要配置我的一些模块在不同的子域工作的以下情形:处理多个子域

  • 我有不同的模块,如API,后端(管理员),应用(核心 模块),博客等。
  • 我有多个虚拟主机,它指向同一个 应用程序/ public目录像api.foobar.com,admin.foobar.com
  • 我使用的唯一每个模块中的控制器键/别名,如下所示:

...

'controllers' => array(
    'invokables' => array(
     'home-controller'  => 'Application\Controller\IndexController', 
    ), 
), 

所以,我想基于域改变我的应用程序的行为(主机名/ routeMatch),如果主机看起来像api.foobar.com我的默认控制器应该得到的形成宁静控制器,如果它是一个通用的主页请求,我认证的管理员控制器不应该调用,如果它是移动站点,则将家庭控制器更改为移动控制器,利用不同的布局等...

非工作配置(应用程序/ config/module.config.php):

return array(
    'router' => array(
     'routes' => array(
      'home' => array(
       'type' => 'hostname', 
       'options' => array(
        'route' => 'www.foobar.com', 
        'defaults' => array(
         '__NAMESPACE__' => 'Application\Controller', 
         'controller' => 'home-controller', 
         'action' => 'index' 
        ) 
       ) 
      ), 
      'api' => array(
       'type' => 'hostname', 
       'options' => array(
        'route' => 'api.foobar.com', 
        'constraints' => array(
         'subdomain' => 'api', 
        ), 
        'defaults' => array(
         '__NAMESPACE__' => 'Api\Controller', 
         'controller' => 'api-controller', 
         'action' => 'index' 
        ) 
       ) 
      ), 
      'admin' => array(
       'type' => 'hostname', 
       'options' => array(
        'route' => 'admin.foobar.com', 
        'constraints' => array(
         'subdomain' => 'admin', 
        ), 
        'defaults' => array(
         '__NAMESPACE__' => 'Admin\Controller', 
         'controller' => 'admin-controller', 
         'action' => 'index' 
        ) 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/[:controller[/:action]]', 
          'constraints' => array(
           '__NAMESPACE__' => 'Admin\Controller', 
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action' => '[a-zA-Z][a-zA-Z0-9_-]*' 
          ), 
          'defaults' => array() 
         ) 
        ) 
       ) 
      ) 
     ) 
    ) 
); 

有趣的是,目前这个配置我得到一个404页面没有发现错误api.foobar.com,www.foobar.com和admin.foobar.com在我的开发环境和转储的路线如下:

object(Zend\Mvc\Router\PriorityList)[214] 
    protected 'routes' => 
    array (size=3) 
     'home' => 
     array (size=3) 
      'route' => 
      object(Zend\Mvc\Router\Http\Hostname)[216] 
       ... 
      'priority' => int 0 
      'serial' => int 0 
     'admin' => 
     array (size=3) 
      'route' => 
      object(Zend\Mvc\Router\Http\Part)[218] 
       ... 
      'priority' => int 0 
      'serial' => int 1 
       ... 
      'priority' => int 0 
      'serial' => int 2 
    protected 'serial' => int 3 
    protected 'count' => int 3 
    protected 'sorted' => boolean false 

我在应用模块> Module.php> onBootstrap($ E)方法简单地得到这个转储: $路径= $ E-> getApplication() - > getServiceManager->获取('路由器“) - > getRoutes();

问题:

  1. 什么不对这个CONFIGRATION?
  2. 为什么我的顶级管理路由列为Zend \ Mvc \ Router \ Http \ Part
  3. 我应该将路由定义分隔到本场景的每个模块自己的module.config.php文件中吗?

回答

1

---什么是错的这个配置

它运作良好,在我的测试,你可以参考代码底部。 因此,我建议你应该检查:

  1. 确保控制器/操作文件存在
  2. 确保虚拟主机已在Apache中已经配置/ nginx的/,无论HTTP主机所使用。

---为什么我的顶级管理路由列表为Zend \ Mvc \ Router \ Http \ Part?

actully,你的“管理员”路线配置了孩子的路线,ZF2将把它作为“部分”的路线,请参阅Zend\Mvc\Router\Http\Part example

---我应该路由定义分开,每个模块本身的module.config这个场景的.php文件?

不,实际上你可以把所有的模块路由放在一个模块的配置文件中,如果你喜欢的话。

这里是我的代码: 返回数组

(
    'router' => array(
     'routes' => array(
       /* 
      'home' => array(
       'type' => 'Zend\Mvc\Router\Http\Literal', 
       'options' => array(
        'route' => '/', 
        'defaults' => array(
         'controller' => 'Application\Controller\Index', 
         'action'  => 'index', 
        ), 
       ), 
      ), 
      */ 
      'home1' => array(
        'type' => 'hostname', 
        'options' => array(
          'route' => 'zfskeleton.com', 
          'defaults' => array(
            '__NAMESPACE__' => 'Application\Controller', 
            'controller' => 'Index', 
            'action' => 'index' 
          ) 
        ) 
      ), 
      'home' => array(
        'type' => 'hostname', 
        'options' => array(
          'route' => 'www.zfskeleton.com', 
          'defaults' => array(
            '__NAMESPACE__' => 'Application\Controller', 
            'controller' => 'Index', 
            'action' => 'index' 
          ) 
        ) 
      ), 
      'api' => array(
        'type' => 'hostname', 
        'options' => array(
          'route' => 'api.zfskeleton.com', 
          'constraints' => array(
            'subdomain' => 'api', 
          ), 
          'defaults' => array(
            '__NAMESPACE__' => 'Album\Controller', 
            'controller' => 'Album', 
            'action' => 'api' 
          ) 
        ) 
      ), 
      'admin' => array(
        'type' => 'hostname', 
        'options' => array(
          'route' => 'admin.zfskeleton.com', 
          'constraints' => array(
            'subdomain' => 'admin', 
          ), 
          'defaults' => array(
            '__NAMESPACE__' => 'Album\Controller', 
            'controller' => 'Album', 
            'action' => 'admin' 
          ) 
        ), 
        'may_terminate' => true, 
        'child_routes' => array(
          'default' => array(
            'type' => 'Segment', 
            'options' => array(
              'route' => '/[:controller[/:action]]', 
              'constraints' => array(
                '__NAMESPACE__' => 'Admin\Controller', 
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*' 
              ), 
              'defaults' => array() 
            ) 
          ) 
        ) 
      ), 
      // The following is a route to simplify getting started creating 
      // new controllers and actions without needing to create a new 
      // module. Simply drop new controllers in, and you can access them 
      // using the path /application/:controller/:action 
      'application' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => '/application', 
        'defaults' => array(
         '__NAMESPACE__' => 'Application\Controller', 
         'controller' => 'Index', 
         'action'  => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        '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(
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ),