Clearly I'm missing something - sessionContainers

Hi,

I’m attempting to get laminas-session to behave for me - and I’m extending the skeleton application, but I can’t seem to get the code to identify the Session container class even though it’s being loaded per the example on Usage in a laminas-mvc application - laminas-session - Laminas Docs

The error I’m getting from within the AlbumController is:

" Additional information:

Laminas\ServiceManager\Exception\ServiceNotFoundException

File:

/data/sites/portal-development/vendor/laminas/laminas-servicemanager/src/AbstractFactory/ReflectionBasedAbstractFactory.php:216

Message:
Unable to create service “Album\Controller\AlbumController”; unable to resolve parameter “sessionContainer” to a class, interface, or array type"

I’m sure it’s something silly I’m doing or not loading. I’m not actually creating a separate ‘instance’ of the container object, but the MVC guide does not allude to that being required (but the main part of the documentation does.) - for the sake of me not going around in circles I felt it best to ask here :slight_smile:
Thanks!

Hey @medge ,

are you sure you have updated the factory for the album controller. Looks like the session container is never reaching the controller. Can you show us the constructor code of your albom controller class as well as the factory for this controller?

Hey @ezkimo

In the global configuration I have the config setup for it;

    'service_manager' => [
        'abstract_factories' => [
            ReflectionBasedAbstractFactory::class,
        ],
        'factories' => [
            'Application\Db\WriteAdapter' => AdapterAbstractServiceFactory::class,
            'Album\Model\Album' => ReflectionBasedAbstractFactory::class,
            'Laminas\Session\Config\ConfigInterface' => 'Laminas\Session\Service\SessionConfigFactory',
            'Laminas\Session\Container' => Container::class
        ],
    ],

The constructor is per the documentation

    private SessionContainer $sessionContainer;

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

Within the Album module configuration, I have these configuration items;

    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => ReflectionBasedAbstractFactory::class,
        ],
    ],

But all the code is primarily configuration related, on the presumption the factory was being created in the background.

Thanks for getting back to me :slight_smile:
Martin.

Hello and welcome to our forums! :smiley:

Everything related to the session is wrong here, also the multiple usage of the ReflectionBasedAbstractFactory. So remove everything for session and also the entry for abstract_factories – if this is not explicitly needed.

Please check the tutorial again, because the complete configuration for the session is included:

No factory is created here because Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory is the factory.
This factory analyses the constructor of your controller, or any other class, and fetches the required dependencies from the service container. So you don’t have to create a factory yourself.

1 Like

Hey (and thanks:))

I did have it like that I thought in the beginning - I will strip it back per your instructions and see what happens - thanks for the advice :slight_smile:

Cheers
Martin

(yes, I’ve deleted the post and changed my password - what I get for doing things in a hurry ;))

Well, after a long distracting break, I’m back on this. I have torn the code back to (mostly, exactly) what is in the example, and I’m still getting this error:

ile:
/data/sites/portal-development/vendor/laminas/laminas-servicemanager/src/AbstractFactory/ReflectionBasedAbstractFactory.php:216

Message:
Unable to create service "Album\Controller\AlbumController"; unable to resolve parameter "sessionContainer" to a class, interface, or array type

In my Album Controller:

    /** @var SessionContainer */
    private SessionContainer $sessionContainer;

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

In my Album/config/module.config.php

 'controllers' => [
        'factories' => [
            Controller\AlbumController::class => ReflectionBasedAbstractFactory::class
        ],

    ],

    // The following section is new and should be added to your file:
    // Stuff
    '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',
        ],
    ],

and in my autoload/global.php:

return [
    'db' => [
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=asmorphic;web-02.asmorphic.com;charset=utf8',
        'username' => 'asmorphic_portal',
        'password' => 'xxxxxxxxx',
    ],
    'service_manager' => [
        'factories' => [
            'Application\Db\WriteAdapter' => AdapterAbstractServiceFactory::class,
            'Album\Model\Album' => ReflectionBasedAbstractFactory::class,
        ],
    ],
    'session_containers' => [
        Laminas\Session\Container::class,
    ],
    'session_storage' => [
        'type' => Laminas\Session\Storage\SessionArrayStorage::class,
    ],
    'session_config'  => [
        'gc_maxlifetime' => 7200,
        // …
    ]
];

Besides the parameter ordering within the Album Constructor - unless I’m missing something, they look completely the same as what is in the documentation. The error reads as if it can’t resolve ‘SessionContainer’ to something that exists.

Also, I presume when I get this working, if i want the session to be database bound, I would be tinkering with the ‘session_storage’ node?

Thanks again,
Martin

Hi @medge,

I believe the problem is simply in copy paste. You’ve missed backslashes when declaring session container.

    'session_containers' => [
        \Laminas\Session\Container::class,
    ],
    'session_storage' => [
        'type' => \Laminas\Session\Storage\SessionArrayStorage::class,
    ],

I hope it helps.

Hey @ALTAMASH80

Thanks for looking into it -

According to this (and PHP Storm auto-resolving it) - there are no leading slashes.

Thanks
Martin.

Can’t argue with AI nowadays. PhpStorm says it is right therefore it is right.

I think it was something to do with auto-deployment and some files initially not being copied. I have done a full fresh upload and it appears to be working now. Inspection is great, but sometimes a hammer is too :wink: