我使用了zend skeleton应用程序,但是当我路由我的模块时,它给出了错误A 404错误 网页未找到。 路由无法匹配请求的URL。 这是我的module.config.php文件代码:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Calendar\Controller\Calendar' => 'Calendar\Controller\CalendarController',
'Calendar\Controller\Event' => 'Calendar\Controller\EventController',
),
),
'router' => array(
'routes' => array(
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Calendar\Controller\Calendar',
'action' => 'index',
),
),
),
'event' => array(
'type' => 'segment',
'options' => array(
'route' => '/event[/:action][/:id][/:unixTime][/:allDay]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
'unixTime' => '[0-9]+',
'allDay' => '0|1',
),
'defaults' => array(
'controller' => 'Calendar\Controller\Event',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'calendar' => __DIR__ . '/../view',
),
),
);
我如何在zf2中设置路由?
答案 0 :(得分:0)
目前尚不清楚您究竟需要什么。但是如果添加一个额外的控制器,那么你必须在root.config文件夹中的application.config.php中添加该控制器。
文件中的编辑该部分并添加您的控制器名称
返回数组(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
"your controller name goes here",
),
答案 1 :(得分:-3)
试试这个,
'defaults' => array(
'__NAMESPACE__' => 'Calendar\Controller',
我不确定这是导致你问题的原因, 'defaults'=&gt;阵列( 'controller'=&gt; “日历\控制器\日历”, 另外我比较了你的模块配置,我注意到你遗漏了一些代码。 例如,在骨架应用程序的默认模块配置中,您可以看到差异, 在开发我的项目时,我使用这个作为指南,所有的路由工作。您可以尝试与它进行比较并查看差异。
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',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(//e.g. YOUR ARE MISSING THIS LINE
'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(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'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'
),
),
'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',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);