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模块未启用且缺少数据库适配器配置,以下是完整解决流程:
启用Laminas\Db模块
修改modules.config.php,将Laminas\Db加入启用模块列表,确保它在自定义模块之前:return [ 'Laminas\Router', 'Laminas\Validator', 'Laminas\Db', // 新增该行 'Application', 'Album', ];配置数据库适配器
在项目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替换为实际的数据库信息。确认数据库表存在
确保目标数据库中已创建album表,表结构参考教程:CREATE TABLE album ( id INT AUTO_INCREMENT PRIMARY KEY, artist VARCHAR(100) NOT NULL, title VARCHAR(100) NOT NULL );排查新增模块后的错误
- 若出现PDO连接错误:检查数据库服务是否正常运行,主机、用户名、密码、数据库名等连接信息是否正确。
- 若出现表不存在错误:确认
album表已在配置的数据库中创建,表名拼写与代码中是否一致。
内容的提问来源于stack exchange,提问作者oskar
相关产品推荐
相关产品推荐

