You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laminas框架教程访问localhost/album时遇ServiceNotFoundException错误

Laminas框架访问/album路径时出现ServiceNotFoundException错误解决

错误信息

错误类型:Laminas\ServiceManager\Exception\ServiceNotFoundException
错误文件:D:\Projects\validation\vendor\laminas\laminas-servicemanager\src\ServiceManager.php:586
错误提示:Unable to resolve service "Laminas\Db\Adapter\AdapterInterface" to a factory; are you certain you provided it during configuration?

相关文件代码

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模块未启用且缺少数据库适配器配置,以下是完整解决流程:

  1. 启用Laminas\Db模块
    修改modules.config.php,将Laminas\Db加入启用模块列表,确保它在自定义模块之前:

    return [
        'Laminas\Router',
        'Laminas\Validator',
        'Laminas\Db', // 新增该行
        'Application',
        'Album',
    ];
    
  2. 配置数据库适配器
    在项目config/autoload目录下创建或修改local.php(建议用该文件存储敏感信息,不要提交到版本库),添加数据库连接配置:

    <?php
    return [
        'db' => [
            'driver' => 'Pdo',
            'dsn' => 'mysql:dbname=your_database_name;host=localhost',
            'username' => 'your_db_username',
            'password' => 'your_db_password',
            'driver_options' => [
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ],
        ],
        'service_manager' => [
            'factories' => [
                \Laminas\Db\Adapter\Adapter::class => \Laminas\Db\Adapter\AdapterServiceFactory::class,
            ],
        ],
    ];
    

    将代码中的your_database_name、your_db_username、your_db_password替换为实际的数据库信息。

  3. 确认数据库表存在
    确保目标数据库中已创建album表,表结构参考教程:

    CREATE TABLE album (
        id INT AUTO_INCREMENT PRIMARY KEY,
        artist VARCHAR(100) NOT NULL,
        title VARCHAR(100) NOT NULL
    );
    
  4. 排查新增模块后的错误

    • 若出现PDO连接错误:检查数据库服务是否正常运行,主机、用户名、密码、数据库名等连接信息是否正确。
    • 若出现表不存在错误:确认album表已在配置的数据库中创建,表名拼写与代码中是否一致。

内容的提问来源于stack exchange,提问作者oskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:37:06