无法将服务“Zend \ Db \ Adapter \ AdapterInterface”解析为工厂

时间:2018-06-07 06:35:40

标签: zend-framework zend-framework2 zend-form zend-db zend-framework3

我是zf的新手。我从github下载了专辑项目并开始研究它。我完成了本教程中编写的所有配置。 但我不断得到这个错误。 Unable to resolve service "Zend\Db\Adapter\AdapterInterface" to a factory; are you certain you provided it during configuration?

我开始寻找一些解决方案,发现在Stack Overflow中发布了同样的问题并通过它们,但没有一个解决方案适合我。下面我粘贴页面代码,请帮助我在这里坚持2天。我正在研究我的本地系统。我安装了Windows 10和xampp。

Module.php

<?php
namespace Album;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    public function getConfig()
    {
        echo "Module.php";
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\AlbumTable::class => function ($container) {
                    $tableGateway = $container->get('Model\AlbumTableGateway');
                    return new Model\AlbumTable($tableGateway);
                },
                'Model\AlbumTableGateway' => function ($container) {
                    $dbAdapter          = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\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)
                    );
                },
            ],
        ];
    }
}

module.config.php     

namespace Album;
use Zend\Router\Http\Segment;

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

    'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

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/",
        "Album\\": "module/Album/src/"
    }
},
"autoload-dev": {
    "psr-4": {
        "ApplicationTest\\": "module/Application/test/",
        "Album\\": "module/Album/src/"
    }
},
"extra": [],
"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"
}
}

global.php

return array(
 'db' => array(
    'driver'   => 'Pdo',
    'adapters' => array(
        'default_db' => array(
            'driver'         => 'Pdo',
            'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
            'username' => 'root',
            'password' => '',
            'driver_options' => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' // optional
            ),
        ),
    ),
),
);

AlbumController.php

<?php
namespace Album\Controller;

use Album\Form\AlbumForm;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


class AlbumController extends AbstractActionController
 {
 private $table;

public function __construct(AlbumTable $table)
{
    $this->table = $table;
}

public function indexAction()
{
    // Grab the paginator from the AlbumTable:
    $paginator = $this->table->fetchAll(true);

    // Set the current page to what has been passed in query string,
    // or to 1 if none is set, or the page is invalid:
    $page = (int) $this->params()->fromQuery('page', 1);
    $page = ($page < 1) ? 1 : $page;
    $paginator->setCurrentPageNumber($page);

    // Set the number of items per page to 10:
    $paginator->setItemCountPerPage(10);

    return new ViewModel(['paginator' => $paginator]);
}

public function addAction()
{
    $form = new AlbumForm();
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();

    if (!$request->isPost()) {
        return ['form' => $form];
    }

    $album = new Album();
    $form->setInputFilter($album->getInputFilter());
    $form->setData($request->getPost());

    if (!$form->isValid()) {
        return ['form' => $form];
    }

    $album->exchangeArray($form->getData());
    $this->table->saveAlbum($album);

    return $this->redirect()->toRoute('album');
}

public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);

    if (0 === $id) {
        return $this->redirect()->toRoute('album', ['action' => 'add']);
    }

    // Retrieve the album with the specified id. Doing so raises
    // an exception if the album is not found, which should result
    // in redirecting to the landing page.
    try {
        $album = $this->table->getAlbum($id);
    } catch (\Exception $e) {
        return $this->redirect()->toRoute('album', ['action' => 'index']);
    }

    $form = new AlbumForm();
    $form->bind($album);
    $form->get('submit')->setAttribute('value', 'Edit');

    $request  = $this->getRequest();
    $viewData = ['id' => $id, 'form' => $form];

    if (!$request->isPost()) {
        return $viewData;
    }

    $form->setInputFilter($album->getInputFilter());
    $form->setData($request->getPost());

    if (!$form->isValid()) {
        return $viewData;
    }

    $this->table->saveAlbum($album);

    // Redirect to album list
    return $this->redirect()->toRoute('album', ['action' => 'index']);
}

public function deleteAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('album');
    }

    $request = $this->getRequest();
    if ($request->isPost()) {
        $del = $request->getPost('del', 'No');

        if ($del == 'Yes') {
            $id = (int) $request->getPost('id');
            $this->table->deleteAlbum($id);
        }

        // Redirect to list of albums
        return $this->redirect()->toRoute('album');
    }

    return [
        'id'    => $id,
        'album' => $this->table->getAlbum($id),
    ];
}
}

2 个答案:

答案 0 :(得分:0)

配置您的"config/autoload/local.php""config/autoload/global.php"

return array(
   'db' => array(
        'driver'   => 'Pdo',
        'adapters' => array(
            'default_db' => array(
                'driver'         => 'Pdo',
                'dsn'            => 'mysql:dbname=YOURDBNAME;host=localhost',
                'username' => 'xxx',
                'password' => 'xxx',
                'driver_options' => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' // optional
                ),
            ),
        ),
    ),
   'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        ),
    ),

);

您可以在"config/autoload/local.php"上设置凭据。

public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\AlbumTable::class => function ($container) {
                    $tableGateway = $container->get('Model\AlbumTableGateway');
                    return new Model\AlbumTable($tableGateway);
                },
                'Model\AlbumTableGateway' => function ($container) {
                    $dbAdapter          = $container->get('default_db'); // return driver instance
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }

config/application.config.php

return array(   
    'modules' => array(          
        'Zend\Db', // make sure you have `'Zend\Db'` to top

更新你的composer.json文件,如下所示,

"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",
    "zendframework/zendframework": "^3.0",
    "zendframework/zend-db": "^2.8.2",
},

用于在下面呈现更新"module.config.php"

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

答案 1 :(得分:0)

我也遇到了同样的问题。我只使用了最小的zend框架安装。

检查composer.json是否已安装 zend-db

如果zend-db不存在,请通过composer安装zend-db,

composer require zendframework/zend-db

在安装过程中,它将要求注入配置文件,为config.modules.php选择1。