zend framework 2 404错误发生。

时间:2022-06-01 18:15:22

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. The requested URL could not be matched by routing. my Album/config/module.config.php code is

我是zend framework 2的新手,尝试将相册模块添加到ZF2的skelton应用程序中,但是没有找到404错误。请求的URL不能与路由匹配。我的相册/ 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:

在应用程序/配置/ module.config。我添加了这些行:

'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...

谁能帮我更正一下代码……

3 个解决方案

#1


1  

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. So you have two options:

__NAMESPACE__将被预先分配给您指定的任何控制器。所以你有两个选择:

  1. Skip the __NAMESPACE__
  2. 跳过__NAMESPACE__
  3. Modify the controller
  4. 修改控制器

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。

#2


0  

You should use configuration like Application module in ZendSkeletonApplication:

您应该在zend骸骨应用程序中使用类似于应用程序模块的配置:

'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]. And now it's worked!

将此代码添加到“子路由”键,然后您将访问url: localhost/模块[/:controller][/:action][/:id]。现在的工作!

#3


0  

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

#1


1  

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. So you have two options:

__NAMESPACE__将被预先分配给您指定的任何控制器。所以你有两个选择:

  1. Skip the __NAMESPACE__
  2. 跳过__NAMESPACE__
  3. Modify the controller
  4. 修改控制器

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。

#2


0  

You should use configuration like Application module in ZendSkeletonApplication:

您应该在zend骸骨应用程序中使用类似于应用程序模块的配置:

'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]. And now it's worked!

将此代码添加到“子路由”键,然后您将访问url: localhost/模块[/:controller][/:action][/:id]。现在的工作!

#3


0  

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