# Missing getServiceLocator method after migration to laminas-mvc 3.x

**URL:** <https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657>\
**Category:** Migration\
**Tags:** laminas-mvc\
**Created:** [June 2, 2020, 10:32pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657 "2020-06-02T22:32:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Pavan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jorge_pavan/32/607_2.png) [@Jorge\_Pavan](https://discourse.laminas.dev/u/Jorge_Pavan)\
**Post date:** [June 2, 2020, 10:32pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/1 "2020-06-02T22:32:28Z")

</div>

I have migrated to Laminas an application that uses Doctrine for the connection to the database.

In it I used  
 _db = this-\> getServiceLocator () -\> get (‘doctrine.connection.orm\_default’);_  
to work on the database.

But now I get this error:  
_A plugin by the name “getServiceLocator” was not found in the plugin manager Laminas \ Mvc \ Controller \ PluginManager_

Could someone tell me how to go about solving it.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 3, 2020, 6:10am UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/2 "2020-06-03T06:10:24Z")

</div>

Hello and welcome to the forums! 😃

> [@Jorge\_Pavan](#):
>
> But now I get this error:  
> _A plugin by the name “getServiceLocator” was not found in the plugin manager Laminas \ Mvc \ Controller \ PluginManager_

The problem is not related to Doctrine but to an older version of zend-mvc:

> laminas-servicemanager v3.0 removes `Laminas\ServiceManager\ServiceLocatorAwareInterface` .

The short answer is to use a factory for your controller. For explanation with code example please look at the migration guide of laminas-mvc: “[Migration – ServiceLocatorAware initializers](https://docs.laminas.dev/laminas-mvc/migration/to-v2-7/#servicelocatoraware-initializers)”

---

<div class="post-metadata">

**Author:** ![Jorge\_Pavan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jorge_pavan/32/607_2.png) [@Jorge\_Pavan](https://discourse.laminas.dev/u/Jorge_Pavan)\
**Post date:** [June 7, 2020, 1:26pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/3 "2020-06-07T13:26:25Z")

</div>

Thanks Frank, i will try that.

---

<div class="post-metadata">

**Author:** ![Jorge\_Pavan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jorge_pavan/32/607_2.png) [@Jorge\_Pavan](https://discourse.laminas.dev/u/Jorge_Pavan)\
**Post date:** [June 7, 2020, 3:23pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/4 "2020-06-07T15:23:27Z")

</div>

I tried it but it doesn’t work.  
This are my files:

_module.php:_

```php
Abm namespace;

use Sheets \ Db \ Adapter \ AdapterInterface;
use Plates \ Db \ ResultSet \ ResultSet;
use Plates \ Db \ TableGateway \ TableGateway;
use Laminates \ ModuleManager \ Feature \ ConfigProviderInterface;

The class module implements ConfigProviderInterface
{
    private $ db;
    public function getConfig ()
    {
        The return includes __DIR__. '/../config/module.config.php';
    }
    public function getControllerConfig ()
    {
        $ db = new AdapterInterface;
        Return [
            'factories' => [
                Controller \ AbmController :: class => function () {
                    return new Controller \ AbmController ($ db);
                },
                Controller \ AbmControllerFactory :: class => function () {
                    return new Controller \ AbmControllerFactory (
                        );
                },
          ],
         ];
    }
}

```

**AbmControllerFactory.php:**

```php
namespace Abm \ Controller;
    
use Interop \ Container \ ContainerInterface;

class AbmControllerFactory
{
    public function __invoke (ContainerInterface $ container)
    {
        return new AbmController ($ container-> get ('Db \ ApplicationAdapter'));
    }
}

```

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [June 11, 2020, 2:55pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/5 "2020-06-11T14:55:30Z")

</div>

The problem is because you’re providing a closure factory directly in your `getControllerConfig()`, and providing one for each of the controller, and the factory. Instead, you should be mapping the controller to its factory within that configuration. Try this instead:

```php
public function getControllerConfig()
{
    return [
        'factories' => [
            Controller\AbmController::class => Controller\AbmControllerFactory::class,
        ],
    ];
}

```

What the above does is tell the controller plugin manager that when you request the `AbmController` class, it should instantiate and consume the `AbmControllerFactory` class in order to create the instance. Your `AbmControllerFactory::__invoke()` method will now be used to do that, and that method is written correctly in terms of pulling dependencies from the container to inject.

---

<div class="post-metadata">

**Author:** ![Jorge\_Pavan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jorge_pavan/32/607_2.png) [@Jorge\_Pavan](https://discourse.laminas.dev/u/Jorge_Pavan)\
**Post date:** [June 13, 2020, 11:32pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/6 "2020-06-13T23:32:51Z")

</div>

I tried it but obtain:

> ```auto
> *Unable to resolve service "Db\ApplicationAdapter" to a factory; are you certain you provided it during configuration?*
> 
> ```

_Jorge Mario Paván_  
_+54 9 11 5327 9247_  
_[jorge.pavan@gmail.com](mailto:jorge.pavan@gmail.com)_

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [June 19, 2020, 2:51pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/7 "2020-06-19T14:51:26Z")

</div>

Have you registered that service in your application? The exception message indicates you haven’t. That’s your next thing to correct.

---

<div class="post-metadata">

**Author:** ![Jorge\_Pavan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jorge_pavan/32/607_2.png) [@Jorge\_Pavan](https://discourse.laminas.dev/u/Jorge_Pavan)\
**Post date:** [June 24, 2020, 7:09pm UTC](https://discourse.laminas.dev/t/missing-getservicelocator-method-after-migration-to-laminas-mvc-3-x/1657/8 "2020-06-24T19:09:30Z")

</div>

Thanks matthew. I did resolve it so:

- putting in `config/autoload/doctrine_orm.local.php` the db connection parameters
- and the `AbmControllerFactory.php` contains:

```php
use Interop\Container\ContainerInterface;
use Doctrine\DBAL\Connection;

class AbmControllerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return new AbmController($container->get('doctrine.connection.orm_default'));
    }
}

```
