繁体   English   中英

zf2中发生404错误

[英]A 404 error occurred in zf2

我使用了zend骨架应用程序,但是当我路由模块时出现错误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中设置路由?

目前尚不清楚您到底需要什么。 但是,如果您额外添加一个控制器,则必须在根目录的config文件夹中的application.config.php中添加该控制器。

在文件中编辑该部分并添加您的控制器名称

返回数组(

// This should be an array of module namespaces used in the application.
'modules' => array(
    'Application',
    "your controller name goes here",
),

尝试这个,

'defaults' => array(

'__NAMESPACE__' => 'Calendar\Controller',

好吧,我不确定这是什么引起您的问题,'defaults'=> array('controller'=>'Calendar \\ Controller \\ Calendar',我也比较了您的模块配置,发现您缺少一些代码。例如,在骨架应用程序的默认模块配置中,您可以看到差异,在开发我的项目时,我以该指南为指导,并且所有的布线工作都可以进行,您可以尝试将其与您的比较并查看差异。

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(
        ),
    ),
),

);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM