2013-09-30 74 views
1

我是新來的Zend Framework 2,並試圖相冊模塊添加到ZF2的骨架應用程序,但越來越 發生404錯誤 找不到網頁。 所請求的網址無法通過路由匹配。 我的專輯/配置/ module.config.php代碼Zend Framework的2 404出錯

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

和應用程序/配置/ 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', 
     ), 
    ), 

誰能plz幫助我糾正碼...

回答

1

問題是你的路由配置中:

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

有了這個,你告訴路由器加載下面的類

Album\Controller\Album\Controller\Album 

__NAMESPACE__將被預置到任何你指定爲controller。所以,你有兩個選擇:

  1. 跳過__NAMESPACE__
  2. 修改controller

雖然這完全是你的,我個人選擇跳過__NAMESPACE__因爲最終所有我們正在做的是用鑰匙和帶我理解事物的方式工作,關鍵是沒有課,所以不應該有一個命名空間:d

0

你需要變化不大傢伙......沒有錯,你的代碼。

命名空間

如果指定命名空間,你必須只包括路由控制器名稱:

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

您應該使用配置在像ZendSkeletonApplication應用模塊:

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

將此代碼添加到「兒童路線」鍵後,你會訪問網址:本地主機/模塊[/:控制器] [ /:動作] [/:ID]。現在它的工作!