首页 文章

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

提问于
浏览
0

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

文件:/var/www/zf2-tutorial/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php:30消息:未找到Class Album / Controller / AlbumController . 堆栈跟踪#0 / var / www / zf2-tutorial / vendor / zendframework / zend-servicemanager / src / ServiceManager.php(758):Zend \ ServiceManager \ Factory \ InvokableFactory- __invoke(对象(Zend \ ServiceManager \ ServiceManager),' Album \ Controlle ...',NULL)#1 / var / www / zf2-tutorial / vendor / zendframework / zend- servicemanager / src / ServiceManager.php(200):Zend \ ServiceManager \ ServiceManager-> doCreate('Album \控制...')#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- t riggerListeners(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

**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 回答

  • 0

    您的配置中存在多个问题 .

    您应该更改注册控制器的方式 . 它现在只能注册为您给出的字符串的invokable . 当您尝试在路线配置中使用它时,它的FQCN未注册 . 然而,您在路由配置中使用了错误的FQCN . 您正在使用 Controller\Album::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

相关问题