I'm doing a laminas framework tutorial and getting some errors trying to access localhost/album

333 Views Asked by At

The error code is Laminas\ServiceManager\Exception\ServiceNotFoundException File: D:\Projects\validation\vendor\laminas\laminas-servicemanager\src\ServiceManager.php:586

The error message is Unable to resolve service "Laminas\Db\Adapter\AdapterInterface" to a factory; are you certain you provided it during configuration?

Module.php file:

<?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 file:

<?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 file:

<?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',
];

The problem seems to be that the tutorial uses Laminas\Db service that isnt there anymore in the modules.config.php file, the tutorial shows a modules.config.php file that is different than mine, I tried adding 'Laminas\Db' there but this just results in a different error.

2

There are 2 best solutions below

0
mdthh On

The Laminas\Db\Adapter\AdapterInterface-Service is initiated by simply adding your specific Db-configuration into your in config/autoload/global.php and/or config/autoload/local.php with the key db.

Example for SQLITE:

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

See: Tutorial part on DB

For examples on how to configure your DB for other adapters like MySql see the docs on DB-Adapters

0
Tyrsson On

Couple of things here.

  1. Yes, the module has to be registered in modules.config.php. I would check and make sure you are using laminas component installer.

  2. As noted by @mdthh one you have the module registered you need a top level config key of 'db' that holds your connection information. In development mode your configuration should be in /config/autoload/local.php so that its not tracked by git and ends up public in your project remote.

  3. Always, during development make sure you verify that you are in development mode by running composer development-enable

It would be very helpful if you would post the error you received when adding the module to modules.config.php.