我正在开发一个zf2项目,我需要配置一些模块,以便在以下场景中处理不同的子域:
...
'controllers' => array(
'invokables' => array(
'home-controller' => 'Application\Controller\IndexController',
),
),
所以,我想基于域(hostname / routeMatch)更改我的应用程序的行为,如果主机名看起来像api.foobar.com我的默认控制器应该从一个宁静的控制器派生,如果它是一个通用的主页请求我的auth支持管理员控制器不应该调用,如果是移动站点,将家庭控制器更改为移动控制器,使用不同的布局等...
非工作配置(application / 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在我的devlepment环境和转储路径如下:
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)方法简单地说: $ routes = $ e-> getApplication() - > getServiceManager-> get('router') - > getRoutes();
问题:
答案 0 :(得分:1)
---此配置有什么问题
在我的测试中效果很好,你可以参考底部的代码。 所以我建议你检查:
---为什么我的顶级管理路由列为Zend \ Mvc \ Router \ Http \ Part?
实际上,您的“管理员”路线已配置子路线,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(
),
),
),
),
),
),
),