dongpu7881 2017-12-07 15:59
浏览 75
已采纳

没有找到Zend框架3类'Album \ Controller \ AlbumController'

enter image description herei'm trying to add a new module named as Album in Zend Skeleton application but however while accessing it through URL it throws an error below.i have spent almost 5 hours but did not find any solution.Any help will be appreciated.

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"
  }
 }
  • 写回答

1条回答 默认 最新

  • douhao123457 2017-12-08 08:17
    关注

    There are several problems within your configuration.

    You should change the way you register your controllers. It is now only registered as an invokable with the string you've given. It is not registered by its FQCN as you try to use it within your route configuration. Nevertheless you use the wrong FQCN within the route config. You are using Controller\Album::class but the class is called: AlbumController so change that to Controller\AlbumController::class.

    This is what your config should've been looking like:

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

    As we've registered an alias you can replace the controller option within the route defaults. You can thus use 'Album\Controller\Album' instead of the FQCN, but I rather use the FQCN within configurations. As it is easier to maintain your code.

    One next thing you might bump into is that you are trying to use getServiceLocator() within your controller but this is no longer supported. Check this out: ZendFramework: ServiceManager - Factories

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行