0
votes

how can i add a default home page in routing?

my route works when i access this url {myapp}/modulename/controllername but when i access this {myapp}/modulename it returns a 404.

How can i fix that?

return array(
    'controllers'   => array(
        'invokables'    => array(
            'Modulename\Controller\Mycontroller'     => 'Modulename\Controller\TheController',
        ),
    ),
    'router' => array(
         'routes' => array(
            'modulename' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/modulename',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Modulename\Controller',
                        'controller'    => 'My',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'thechild' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '[/:controller][/:action][/:id]',
                            'constraints' => array(
                                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'id'         => '[0-9]*',
                            ),
                            'defaults' => array( 
                                '__NAMESPACE__' => 'Modulename\Controller',
                                'controller'    => 'User',
                                'action'        => 'index',
                            ),
                        ),
                    ),
                ),
            ), 
         ),
     ),  


        'view_manager'  => array(
            'template_path_stack' => array(
                'users' => __DIR__.'/../view',
            ),
            'display_exceptions' => true,
        ),
    );
3
what is that thechild under child routes:change this to 'default' => ....Jangya satapathy

3 Answers

1
votes
'Modulename\Controller\Mycontroller'     => 'Modulename\Controller\TheController',

Here you used the controller name is 'TheController'

   'defaults' => array(
                    '__NAMESPACE__' => 'Modulename\Controller',
                    'controller'    => 'My',
                    'action'        => 'index',
                ),

But here you specified controller name 'My'.

Change here 'controller' => 'The', and check

If your controller name is "UserController" then change

'Modulename\Controller\Mycontroller'     => 'Modulename\Controller\TheController',

 as

'Modulename\Controller\User'     => 'Modulename\Controller\UserController', 

 and 

'defaults' => array(
                    '__NAMESPACE__' => 'Modulename\Controller',
                    'controller'    => 'My',
                    'action'        => 'index',
                ),

  as

'defaults' => array(
                    '__NAMESPACE__' => 'Modulename\Controller',
                    'controller'    => 'User',
                    'action'        => 'index',
                ),

And change this(at after 'may_terminate')

'defaults' => array( 
                    '__NAMESPACE__' => 'Modulename\Controller',
                    'controller'    => 'User',
                    'action'        => 'index',
                    ),
                        ),

as

'defaults' => array(),
1
votes

you dont have any controller named My in 'controller' => 'My',

you should change the controller invokable name :

'controllers'   => array(
    'invokables'    => array(
        'My'     => 'Modulename\Controller\TheController',
    ),
),

or change the default controller in routes definition to

'defaults' => array(
    '__NAMESPACE__' => 'Modulename\Controller',
    'controller'    => 'Modulename\Controller\Mycontroller',
    'action'        => 'index',
),

and this route definition is not correct either

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

it should be

'route'    => '[/:controller[/:action[/:id]]]',
0
votes

thank you for your answers, i found an another way by putting some code in the module.php

Here's my code:

if($e->getRouteMatch()->getMatchedRouteName() == 'modulename') {
    $response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/modulename/user/index');
    $response->setStatusCode(302);
}