简体   繁体   English

zend framework 2 404错误发生

[英]zend framework 2 404 error occured

i'm new to zend framework 2 and was trying to add the album module into ZF2's skelton application but getting A 404 error occurred Page not found. 我是zend Framework 2的新手,正在尝试将相册模块添加到ZF2的skelton应用程序中,但出现A 404错误。找不到页面。 The requested URL could not be matched by routing. 路由无法匹配请求的URL。 my Album/config/module.config.php code is 我的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',
                        ),
                    ),
                ),
            ),
        ),
    );

and in Application/config/module.config.php i have added these lines: 在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',
        ),
    ),

can anyone plz help me to correct the codes... 谁能帮助我更正密码...

The Problem is within your Route-Configuration: 问题在您的路由配置内:

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

With this you tell the Router to load the following class 这样,您告诉路由器加载以下类

Album\Controller\Album\Controller\Album

The __NAMESPACE__ will be prepended to whatever you assign as controller . __NAMESPACE__将放在您指定为controller任何内容之前。 So you have two options: 因此,您有两种选择:

  1. Skip the __NAMESPACE__ 跳过__NAMESPACE__
  2. Modify the controller 修改controller

While this is completely up to you, personally I choose to skip the __NAMESPACE__ since ultimately all we're doing is working with keys and with the way i understand things, keys are no classes and therefore shouldn't have a namespace :D 虽然这完全取决于您,但是我个人选择跳过__NAMESPACE__因为最终我们要做的只是使用键和我了解事物的方式,键不是类,因此不应具有名称空间:D

You need little changes dude...nothing wrong with your code. 伙计,您几乎不需要任何更改……您的代码没有错。

NameSpace 命名空间

If You specify Namespace, you have to include only the controller name in routing: 如果指定名称空间,则必须在路由中仅包含控制器名称:

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

If no namespace included 如果不包括名称空间

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

You should use configuration like Application module in ZendSkeletonApplication: 您应该在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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

You just add the following code: 您只需添加以下代码:

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

add this code to 'child-routes' key and after that you'll access to url: localhost/module[/:controller][/:action][/:id]. 将此代码添加到“ child-routes”键中,然后您将访问url:localhost / module [/:controller] [/:action] [/:id]。 And now it's worked! 现在工作了!

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

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