I'm working on a zf2 project and i need to configure some of my modules to work on different subdomains following scenario:
- I have different Modules like API, Backend (Admin), Application (Core module), Blog etc..
- I have multiple vhosts which points to same application/public directory like api.foobar.com, admin.foobar.com
- I'm using unique controller keys/aliases in each module like this:
...
'controllers' => array(
'invokables' => array(
'home-controller' => 'Application\Controller\IndexController',
),
),
So, i want to change my application's behaviour based on domain (hostname/routeMatch), if hostname looks like api.foobar.com my default controller should derived form a restful controller, if it's a generic homepage request my auth-backed admin controller should not invoke, if it's a mobile site, change home controller to mobile-controller, utilize different layout etc...
Non-working configuration (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()
)
)
)
)
)
)
);
Interesting thing is, currently with this configuration i'm getting a 404 page not found error both api.foobar.com, www.foobar.com and admin.foobar.com on my devlepment environment and dump of routes like following:
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
I got this dump in Application Module > Module.php > onBootstrap($e) method simply: $routes = $e->getApplication()->getServiceManager->get('router')->getRoutes();
Questions:
- What is wrong with this configration?
- why my top-level admin route listing as a Zend\Mvc\Router\Http\Part?
- Should i separate route definitions to every module's own module.config.php file for this scenario?