Zend 框架 3 Class 'Album\Controller\AlbumController' 未找到

Zend framework 3 Class 'Album\Controller\AlbumController' not found

enter image description here我正在尝试在 Zend Skeleton 应用程序中添加一个名为 Album 的新模块,但是在通过 URL 访问它时会抛出错误 below.i已经花了将近 5 个小时,但没有找到任何 solution.Any 帮助将不胜感激。

File:/var/www/zf2-tutorial/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php:30

Message:Class Album/Controller/AlbumController not found.

stack trace #0 /var/www/zf2-tutorial/vendor/zendframework/zend- servicemanager/src/ServiceManager.php(758): Zend\ServiceManager\Factory\InvokableFactory- __invoke(Object(Zend\ServiceManager\ServiceManager), 'Album\Controlle...', NULL) #1 /var/www/zf2-tutorial/vendor/zendframework/zend- servicemanager/src/ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('Album\Controlle...') #2 /var/www/zf2-tutorial/vendor/zendframework/zend- servicemanager/src/AbstractPluginManager.php(141): Zend\ServiceManager\ServiceManager->get('Album\Controlle...') #3 /var/www/zf2-tutorial/vendor/zendframework/zend-
mvc/src/DispatchListener.php(95): Zend\ServiceManager\AbstractPluginManager->get('Album\Controlle...') #4 /var/www/zf2-tutorial/vendor/zendframework/zend- eventmanager/src/EventManager.php(322): Zend\Mvc\DispatchListener- onDispatch(Object(Zend\Mvc\MvcEvent)) #5 /var/www/zf2-tutorial/vendor/zendframework/zend- eventmanager/src/EventManager.php(179): Zend\EventManager\EventManager- triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure)) #6 /var/www/zf2-tutorial/vendor/zendframework/zend- mvc/src/Application.php(332): Zend\EventManager\EventManager- triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent)) #7 /var/www/zf2-tutorial/public/index.php(48): Zend\Mvc\Application- run() #8 {main}

**module.config.php**
<?php
namespace Album;

  return array(
  'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => Controller\AlbumController::class

    ),
),

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'defaults' => array(
                    'controller' => Controller\Album::class,
                    'action'     => 'index',
                ),
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),

            ),
        ),
    ),
),

'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),
);
 ?>


**AlbumController.php**
<?php
namespace Album;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
protected $albumTable;
public function getAlbumTable()
{
    if (! $this->albumTable) {
        $sm = $this->getServiceLocator();
        $this->albumTable = $sm->get('Album\Model\AlbumTable');
    }
    return $this->albumTable;
}

public function indexAction()
{
    return new ViewModel(array(
        'albums' => '',
    ));
}

public function addAction()
{

}

public function editAction()
{

}

public function deleteAction()
{

}

}


**Album.php**
 <?php
namespace Album\Model;

class Album
{

public $id;

public $artist;

public $title;

public function exchangeArray($data)
{
    $this->id = (! empty($data['id'])) ? $data['id'] : null;
    $this->artist = (! empty($data['artist'])) ? $data['artist'] : 
     null;
    $this->title = (! empty($data['title'])) ? $data['title'] : null;
 } 
 }

 **AlbumTable.php**
<?php
 use Zend\Db\TableGateway\TableGateway;

class AlbumTable
{

protected $tableGateway;

public function __construct(TableGateway $tableGateway)
{
    $this->tableGateway = $tableGateway;
}

public function fetchAll()
{
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}

public function getAlbum($id)
{
    $id = (int) $id;
    $rowset = $this->tableGateway->select(array(
        'id' => $id
    ));
    $row = $rowset->current();
    if (! $row) {
        throw new \Exception("Could not find row $id");
    }
    return $row;
}

public function saveAlbum(Album $album)
{
    $data = array(
        'artist' => $album->artist,
        'title' => $album->title
    );

    $id = (int) $album->id;
    if ($id == 0) {
        $this->tableGateway->insert($data);
    } else {
        if ($this->getAlbum($id)) {
            $this->tableGateway->update($data, array(
                'id' => $id
            ));
        } else {
            throw new \Exception('Album id does not exist');
        }
    }
}

public function deleteAlbum($id)
{
    $this->tableGateway->delete(array(
        'id' => (int) $id
    ));
  }
  }

   **Module.php**
<?php
 namespace Album;

 use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
 use Zend\ModuleManager\Feature\ConfigProviderInterface;
 u    se Album\Model\Album;
 use Album\Model\AlbumTable;
 use Zend\Db\ResultSet\ResultSet;
 use Zend\Db\TableGateway\TableGateway;

 class Module implements AutoloaderProviderInterface, 
 ConfigProviderInterface
   {

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php'
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
            )
        )

    );
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';

}

public function getServiceConfig()
{
     return array(
        'factories' => array(
            'Album\Model\AlbumTable' => function ($sm) {
                $tableGateway = $sm->get('AlbumTableGateway');
                $table = new AlbumTable($tableGateway);
                return $table;
            },
            'AlbumTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new 
              Album());
                return new TableGateway('album', $dbAdapter, null, 
     $resultSetPrototype);
            }
        )
    );
}
public function getControllerConfig()
 {
    return [
        'factories' => [
            Controller\AlbumController::class => function($container) 
  {
                return new Controller\AlbumController(
                    $container->get(Model\AlbumTable::class)
                    );
            },
            ],
            ];
  }
 }


  ?>
 **composer.json**


{
"name" : "zendframework/skeleton-application",
"description" : "Skeleton Application for Zend Framework zend-mvc 
 applications",
"type" : "project",
"license" : "BSD-3-Clause",
"keywords" : [
    "framework",
    "mvc",
    "zf"
],
"homepage" : "http://framework.zend.com/",
"minimum-stability" : "dev",
"prefer-stable" : true,
"require" : {
    "php" : "^5.6 || ^7.0",
    "zendframework/zend-component-installer" : "^1.0 || ^0.7 || 
 ^1.0.0-dev@dev",
    "zendframework/zend-mvc" : "^3.0.1",
    "zfcampus/zf-development-mode" : "^3.0"
},
"autoload" : {
    "psr-4" : {
        "Application\" : "module/Application/src/"

 }
},
"autoload-dev" : {
    "psr-4" : {
        "ApplicationTest\" : "module/Application/test/"
    }
},
"extra" : {
    "zend-skeleton-installer" : [{
            "name" : "zendframework/zend-developer-tools",
            "constraint" : "^1.1.0",
            "prompt" : "Would you like to install the developer 
   toolbar?",
            "module" : true,
            "dev" : true
        }, {
            "name" : "zendframework/zend-cache",
            "constraint" : "^2.7.1",
            "prompt" : "Would you like to install caching support?",
            "module" : true
        }, {
            "name" : "zendframework/zend-db",
            "constraint" : "^2.8.1",
            "prompt" : "Would you like to install database support 
    (installs zend-db)?",
            "module" : true
        }, {
            "name" : "zendframework/zend-mvc-form",
            "constraint" : "^1.0",
            "prompt" : "Would you like to install forms support?",
            "module" : true
        }, {
            "name" : "zendframework/zend-json",
            "constraint" : "^3.0",
            "prompt" : "Would you like to install JSON 
     de/serialization support?"
        }, {
            "name" : "zendframework/zend-log",
            "constraint" : "^2.9",
            "prompt" : "Would you like to install logging support?",
            "module" : true
        }, {
            "name" : "zendframework/zend-mvc-console",
            "constraint" : "^1.1.10",
            "prompt" : "Would you like to install MVC-based console 
  support? (We recommend migrating to zf-console, symfony/console, or 
  Aura.CLI)",
            "module" : true
        }, {
            "name" : "zendframework/zend-mvc-i18n",
            "constraint" : "^1.0",
            "prompt" : "Would you like to install i18n support?",
            "module" : true
        }, {
            "name" : "zendframework/zend-mvc-plugins",
            "constraint" : "^1.0.1",
            "prompt" : "Would you like to install the official MVC 
  plugins, including PRG support, identity, and flash messages?",
            "module" : true
        }, {
            "name" : "zendframework/zend-psr7bridge",
            "constraint" : "^0.2.2",
            "prompt" : "Would you like to use the PSR-7 middleware 
   dispatcher?"
        }, {
            "name" : "zendframework/zend-session",
            "constraint" : "^2.7.1",
            "prompt" : "Would you like to install sessions support?",
            "module" : true
        }, {
            "name" : "zendframework/zend-test",
            "constraint" : "^3.0.1",
            "prompt" : "Would you like to install MVC testing 
     support?",
            "dev" : true
        }, {
            "name" : "zendframework/zend-servicemanager-di",
            "constraint" : "^1.0",
            "prompt" : "Would you like to install the zend-di 
  integration for zend-servicemanager?",
            "module" : true
        }
    ]
  },
"scripts" : {
    "cs-check" : "phpcs",
    "cs-fix" : "phpcbf",
    "development-disable" : "zf-development-mode disable",
    "development-enable" : "zf-development-mode enable",
    "development-status" : "zf-development-mode status",
    "post-create-project-cmd" : "@development-enable",
    "serve" : "php -S 0.0.0.0:8080 -t public public/index.php",
    "test" : "phpunit"
  }
 }

您的配置有几个问题。

您应该更改注册控制器的方式。它现在仅注册为可调用的字符串,您提供的字符串。当您尝试在路由配置中使用它时,它未由其 FQCN 注册。然而,您在路由配置中使用了错误的 FQCN。您正在使用 Controller\Album::class 但 class 被称为:AlbumController 所以将其更改为 Controller\AlbumController::class.

您的配置应该是这样的:

return array(
    'controllers' => array(
        'aliases' => array(
            'Album\Controller\Album' => Controller\AlbumController::class
        ),
        'factories' => array(
            Controller\AlbumController::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
        ),
    ),

    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/:action][/:id]',
                    'defaults' => array(
                        'controller' => Controller\AlbumController::class,
                        'action'     => 'index',
                    ),
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),

                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

由于我们已经注册了一个别名,您可以替换路由默认值中的控制器选项。因此,您可以使用 'Album\Controller\Album' 而不是 FQCN,但我宁愿在配置中使用 FQCN。因为它更容易维护您的代码。

您可能会遇到的下一件事是您正尝试在控制器中使用 getServiceLocator(),但这不再受支持。看看这个:ZendFramework: ServiceManager - Factories