繁体   English   中英

zend framework 2 404错误发生

[英]zend framework 2 404 error occured

我是zend Framework 2的新手,正在尝试将相册模块添加到ZF2的skelton应用程序中,但出现A 404错误。找不到页面。 路由无法匹配请求的URL。 我的Album / config / module.config.php代码是

<?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中,我添加了以下几行:

'modules' => array(
        'Application',    
        'Album'
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),

谁能帮助我更正密码...

问题在您的路由配置内:

'defaults => array(
    '__NAMESPACE__' => 'Album\Controller',
    'controller'    => 'Album\Controller\Album',
)

这样,您告诉路由器加载以下类

Album\Controller\Album\Controller\Album

__NAMESPACE__将放在您指定为controller任何内容之前。 因此,您有两种选择:

  1. 跳过__NAMESPACE__
  2. 修改controller

虽然这完全取决于您,但是我个人选择跳过__NAMESPACE__因为最终我们要做的只是使用键和我了解事物的方式,键不是类,因此不应具有名称空间:D

伙计,您几乎不需要任何更改……您的代码没有错。

命名空间

如果指定名称空间,则必须在路由中仅包含控制器名称:

'defaults' => array(
    '__NAMESPACE__' => 'Album\Controller',
    'controller' => 'Album' /* Not like this: 'Album\Controller\Album' */
    'action' => 'index',
),

如果不包括名称空间

'defaults' => array(
    'controller' => 'Album\Controller\Album' /* Include this */
    'action' => 'index',
 ),

您应该在ZendSkeletonApplication中使用类似Application模块的配置:

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

您只需添加以下代码:

'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] [/:action] [/:id]。 现在工作了!

暂无
暂无

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

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