簡體   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