1
votes

I just created a ZF2 application from Zend Skeleton Application. The default routing of the skeleton needs the Module name in the URL. For example, to invoke index action in index controller in application module, i have to type the url like http://localhost/application/index/index.

i want to omit the module name from the url (because i will only need one module for what i have in mind) so that if i type http://localhost/index/index, i would get the same result.

i tried changing the default module.config.php file in application/config like this -

<?php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\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(   
                                'action' => 'index',
                                '__NAMESPACE__' => 'Application\Controller'                             
                            ),
                        ),
                    ),
                ),
            ),            
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Home' => 'Application\Controller\HomeController',
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

but it's not working. can anyone help me?

2

2 Answers

4
votes

Try to remove the slash from your child route. That is, /[:controller[/:action]] should be [:controller[/:action]]. Then it should work.

If you are interested in a walkthrough of the solution, I wrote an article about it some months ago.

0
votes

I do the trick with this route definition in 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',
                ),
            ),
        ),
        'application' => array(
            'type' => 'segment',
            'options' => array(
                'route'    => '[/:controller[/:action]][/]',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),  
            ),
        ), 
    ),
),

With this line only use controller and action name without module

'route'    => '[/:controller[/:action]][/]'