【问题标题】:A 404 error occurred in zf2zf2出现404错误
【发布时间】:2014-03-27 14:20:06
【问题描述】:

我使用了 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 中设置路由?

【问题讨论】:

  • 你的路由无论如何都不能被路由匹配。您有两个具有相同约束的可选参数,路由器将无法知道是哪个参数。

标签: php zend-framework2


【解决方案1】:

尚不清楚您究竟需要什么。但是如果你额外添加一个控制器,那么你必须在 application.config.php 中添加该控制器,它位于根目录的 config 文件夹中。

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

返回数组(

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

【讨论】:

    【解决方案2】:

    试试这个,

    'defaults' => array(
    
    '__NAMESPACE__' => 'Calendar\Controller',
    

    我不确定这是导致您的问题的原因, '默认值' => 数组( '控制器' => '日历\控制器\日历', 我还比较了您的模块配置,发现您缺少一些代码。 例如,在骨架应用程序的默认模块配置中,您可以看到差异, 在开发我的项目时,我用这个作为指导,所有的路由都有效。你可以试着和你的比较一下,看看有什么不同。

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

    );

    【讨论】:

    • 您能否解释一下这段代码(在您的回答中)?这样你可能会得到更多的支持!
    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 2012-06-30
    • 2012-07-13
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多