如何在路由中添加默认主页?
当我访问此URL {myapp} / modulename / controllername时,我的路由有效 但当我访问此{myapp} / modulename时,它返回404。
我该如何解决?
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, ), );
答案 0 :(得分:1)
'Modulename\Controller\Mycontroller' => 'Modulename\Controller\TheController',
这里使用的控制器名称是'TheController'
'defaults' => array(
'__NAMESPACE__' => 'Modulename\Controller',
'controller' => 'My',
'action' => 'index',
),
但是你在这里指定了控制器名称'My'。
此处更改'controller'=> ''',并检查
如果你的控制器名称是“UserController”,那么改变
'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',
),
并改变它(在'may_terminate'之后)
'defaults' => array(
'__NAMESPACE__' => 'Modulename\Controller',
'controller' => 'User',
'action' => 'index',
),
),
as
'defaults' => array(),
答案 1 :(得分:1)
My
'controller' => 'My',
的控制器
您应该更改控制器可调用名称:
'controllers' => array(
'invokables' => array(
'My' => 'Modulename\Controller\TheController',
),
),
或将路径定义中的默认控制器更改为
'defaults' => array(
'__NAMESPACE__' => 'Modulename\Controller',
'controller' => 'Modulename\Controller\Mycontroller',
'action' => 'index',
),
并且此路线定义也不正确
'route' => '[/:controller][/:action][/:id]',
应该是
'route' => '[/:controller[/:action[/:id]]]',
答案 2 :(得分:0)
感谢您的回答,我通过在module.php
中添加一些代码找到了另一种方法这是我的代码:
if($e->getRouteMatch()->getMatchedRouteName() == 'modulename') { $response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/modulename/user/index'); $response->setStatusCode(302); }