我正在做层压框架教程,并在尝试访问 localhost/album 时遇到一些错误

问题描述 投票:0回答:1

错误代码为 Laminas\ServiceManager\Exception\ServiceNotFoundException 文件: D:\项目验证endor\laminas\laminas-servicemanager\src\ServiceManager.php:586

错误消息为无法将服务“Laminas\Db\Adapter\AdapterInterface”解析为工厂;您确定您在配置过程中提供了它吗?

Module.php 文件:

<?php
namespace Album;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
    public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\AlbumTable::class => function($container) {
                    $tableGateway = $container->get(Model\AlbumTableGateway::class);
                    return new Model\AlbumTable($tableGateway);
                },
                Model\AlbumTableGateway::class => 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 文件:

<?php
namespace Album;
use Laminas\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',
        ],
    ],
];

modules.config.php 文件:

<?php

/**
 * List of enabled modules for this application.
 *
 * This should be an array of module namespaces used in the application.
 */
return [
    'Laminas\Router',
    'Laminas\Validator',
    'Application',
    'Album',
];

问题似乎是教程使用的 Laminas\Db 服务不再存在于modules.config.php文件中,教程显示的modules.config.php文件与我的不同,我尝试添加'Laminas\Db ' 在那里,但这只会导致不同的错误。

php zend-framework laminas
1个回答
0
投票

Laminas\Db\Adapter\AdapterInterface-Service 只需使用密钥

db
将您的特定 Db 配置添加到您的
config/autoload/global.php
和/或 config/autoload/local.php 即可启动。

SQLITE 示例:

return [
    'db' => [
        'driver' => 'Pdo',
        'dsn'    => sprintf('sqlite:%s/data/laminastutorial.db', realpath(getcwd())),
    ],
];

参见:DB 教程部分

有关如何为其他适配器(例如 MySql)配置数据库的示例,请参阅有关 DB-Adapters 的文档

© www.soinside.com 2019 - 2024. All rights reserved.