首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发生zend framework 2 404错误

发生zend framework 2 404错误
EN

Stack Overflow用户
提问于 2013-09-30 13:09:42
回答 3查看 4.4K关注 0票数 1

我是zend Framework2的新手,我试图将相册模块添加到ZF2的skelton应用程序中,但出现了404错误,Page not found。请求的URL无法通过路由匹配。我的相册/config/module.config.php代码是

代码语言:javascript
复制
<?php
    return array(
        'controllers' => array(
            'invokables' => array(
                'Album\Controller\Album' => 'Album\Controller\AlbumController',
            ),
        ),
        'view_manager' => array(
            'template_path_stack' => array(
                'album' => __DIR__ . '/../view'
             ),
         ),
        'router' => array(
            'routes' => array(
                'album' => array(
                    //'type' => 'segment',
                    'type' => 'Zend\Mvc\Router\Http\Literal',
                    'options' => array(
                        //'route' => '/album[/][:action][/:id]',
                        //'route'       => '/:controller[.:formatter][/:id]',
                        'route' => '/album',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'formatter'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id' => '[0-9]+',
                        ),
                        'defaults' => array(
                             '__NAMESPACE__' => 'Album\Controller',
                            'controller' => 'Album\Controller\Album',
                            'action' => 'index',
                        ),
                    ),
                ),
            ),
        ),
    );

在Application/config/module.config.php中,我添加了以下几行:

代码语言:javascript
复制
'modules' => array(
        'Application',    
        'Album'
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),

有没有人能帮我改正代码?

EN

回答 3

Stack Overflow用户

发布于 2013-09-30 13:56:08

问题出在您的路由配置中:

代码语言:javascript
复制
'defaults => array(
    '__NAMESPACE__' => 'Album\Controller',
    'controller'    => 'Album\Controller\Album',
)

这样,您就可以告诉路由器装入以下类

代码语言:javascript
复制
Album\Controller\Album\Controller\Album

__NAMESPACE__将优先于您指定为controller的任何内容。所以你有两个选择:

跳过__NAMESPACE__

  • Modify controller

虽然这完全由您决定,但就我个人而言,我选择跳过__NAMESPACE__,因为最终我们所做的一切都是使用键,根据我对事物的理解,键不是类,因此不应该有名称空间:D

票数 1
EN

Stack Overflow用户

发布于 2013-10-05 18:05:58

你需要在你的代码中做一些dude...nothing错误的小改动。

NameSpace

如果指定Namespace,则路由中只需包含控制器名称:

代码语言:javascript
复制
'defaults' => array(
    '__NAMESPACE__' => 'Album\Controller',
    'controller' => 'Album' /* Not like this: 'Album\Controller\Album' */
    'action' => 'index',
),

如果不包含命名空间,则返回

代码语言:javascript
复制
'defaults' => array(
    'controller' => 'Album\Controller\Album' /* Include this */
    'action' => 'index',
 ),
票数 0
EN

Stack Overflow用户

发布于 2014-04-22 21:57:56

您应该使用类似于ZendSkeletonApplication中的应用程序模块的配置:

代码语言:javascript
复制
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/album',
                'defaults' => array(
                    '__NAMESPACE__' => 'Album\Controller',
                    'controller'    => 'Album',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

您只需添加以下代码:

代码语言:javascript
复制
'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__' => 'Album\Controller',
                            'controller'    => 'Album',
                            'action'        => 'index',
                        ),
                    ),
                ),

将此代码添加到'child-routes‘键,然后您将访问url: localhost/module/:controller/:id。现在它成功了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19086797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档