php - Zend Framework 2 Module to Sub domain mapping

449

I am working on a project with two modules as Web and Cms. I want to use sub domains with these modules.

mydomain.com -> Web Module

cms.mydomain.com -> Cms Module

I used below module.config.php files in each modules

Web Module -> module.config.php

<?php

return array(
     'controllers' => array(
         'invokables' => array(
             'Web\Controller\Index' => 'Web\Controller\IndexController',
         ),
     ),
     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'home' => array(
                'type' => 'Hostname',
                'options' => array(
                    'route' => 'mydomain.com',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Web\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(
                                'controller' => 'Index',
                                'action'     => 'index',
                            ),
                        ),
                    ),
                ),
            ),

         ),
     ),


     'view_manager' => array(
        'template_map' => array(
             'layout/layout' => __DIR__ . '/../view/layout/default.phtml',
        ),
         'template_path_stack' => array(
            'web' => __DIR__ . '/../view',
         ),
     ),
     'session' => array(
        'remember_me_seconds' => 2419200,
        'use_cookies' => true,
        'cookie_httponly' => true,
    ),
 );


?>

CMS Module -> module.config.php

<?php
return array(
     'controllers' => array(
         'invokables' => array(
             'Cms\Controller\Index' => 'Cms\Controller\IndexController',
             'Cms\Controller\User' => 'Cms\Controller\UserController',
         ),
     ),
     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
            'cms' => array(
                'type' => 'Hostname',
                'options' => array(
                    'route' => 'cms.mydomain.dev',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Cms\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(
                                'controller' => 'Index',
                                'action'     => 'index',
                            ),
                        ),
                    ),
                ),
             ),

         ),
     ),


     'view_manager' => array(
        'template_map' => array(
            'layout/default' => __DIR__ . '/../view/layout/default.phtml',
            'layout/system' => __DIR__ . '/../view/layout/system_layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error'               => __DIR__ . '/../view/error/index.phtml',
        ),
         'template_path_stack' => array(
             'cms' => __DIR__ . '/../view/script',
         ),
     ),
     'session' => array(
        'remember_me_seconds' => 2419200,
        'use_cookies' => true,
        'cookie_httponly' => true,
    ),
 );
?>

This doesn't work as expected. Web modulemydomain.com shows it's layout but view content not loading. CMS also showing the web module when I trycms.mydomain.com. Could you please guide me where the issue is.

925

Answer

Solution:

The router config looks OK to me. You may have just a typo in the domain name. In web module, it is mydomain.com whereas in cms module you have some mydomain.dev.

Web module view content not showing may be caused by another reason which has nothing to do with routing.

People are also looking for solutions to the problem: linux - Can't install old version of php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.