在ZF2中发生404错误“请求的URL无法通过路由匹配”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ZF2中发生404错误“请求的URL无法通过路由匹配”相关的知识,希望对你有一定的参考价值。

请注意“发生404错误”无法通过路由“在ZF2中”匹配请求的URL。

给我带来麻烦的部分是这样的:

'router'          => array(
    'routes' => array(
        'album' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'       => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults'    => array(
                    '__NAMESPACE__' => 'AlbumController',
                    'controller'    => 'AlbumControllerAlbum',
                    'action'        => 'index',
                ),
            ),
        ),
    ),
),
答案

你的路线是对的! (默认情况下不需要NameSpace)

// The following section is new and should be added to your file
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'AlbumControllerAlbum',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),

你必须在application.config.php中添加你的模块(你说你已经完成了它。)

// This should be an array of module namespaces used in the application.
'modules' => array(
    'Application',
    'Album'
),
另一答案

1)在类型中使用“段”而不是“段”。 2)在路线上 使用route'=>'/ album [/] [:action] [/:id]',

而不是'route'=>'/ album [/:action] [/:id]',

3)

另一答案

// getAutoloaderConfig() and getConfig() methods here
    public function getAutoloaderConfig() { 
        return array( 
            'ZendLoaderStandardAutoloader' => array( 
                'namespaces' => array( 
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
                ), 
            ), 
        ); 
    } 

    public function getConfig() { 
        return include __DIR__ . '/config/module.config.php'; 
    } 

Module.php

以上是关于在ZF2中发生404错误“请求的URL无法通过路由匹配”的主要内容,如果未能解决你的问题,请参考以下文章