API Tools Admin UI: Cannot create the API module

Hey everybody!

I built a Laminas MVC web app and have to add an API. After some reasearch, I reckoned that the Laminas API Tools UI should be the best way for this. I am working in a vagrant box with composer and managed to set everything up so that I can access the Laminas API Tools UI main screen.

When I click on New API and insert some name like “NewAPI”, I see the following alert:

I cannot create the API module, please check if already exists

There is no API named like this yet and despite the error message a folder NewAPI has been created in module folder. Its structure looks okay to me:

Bildschirmfoto_2021-08-28_10-56-51

The line ‘NewAPI’ gets automatically added to modules.config.php. As I’ve read this post (Laminas API Tools - unable to create new API in Admi UI - #2 by gary), I knew that this should not be the case. So if I remove this new line, API Tools Admin UI can be reloaded normally, but the API list is still empty.

Has anyone got an idea on what I need to change in order to create, see and manage my APIs? That’d be we great! :smiley:


BTW: If I don’t remove the line ‘NewAPI’ from modules.config.php, the following error occurs:

Fatal error: Uncaught Laminas\ModuleManager\Exception\RuntimeException: Module (NewAPI) could not be initialized. in /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php:210 Stack trace: #0 /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php(182): Laminas\ModuleManager\ModuleManager->loadModuleByName(Object(Laminas\ModuleManager\ModuleEvent)) #1 /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php(104): Laminas\ModuleManager\ModuleManager->loadModule(‘NewAPI’) #2 /vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php(331): Laminas\ModuleManager\ModuleManager->onLoadModules(Object(Laminas\ModuleManager\ModuleEvent)) #3 /vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php(180): Laminas\EventManager\EventManager->triggerListeners(Object(Laminas\ModuleManager\ModuleEvent)) #4 /vagrant_ in /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php on line 210

When I remove the line from modules.config.php after this error message, another error is displayed:

Parse error: syntax error, unexpected ‘‘NewAPI’,’ (T_ENCAPSED_AND_WHITESPACE), expecting ‘)’ in /vagrant__my_app/MyApp/config/modules.config.php on line 45

In order to get back to the normal Admin Tools UI screen, I have to add the line in modules.config.php again, reload the browser (which gives me almost the same fatal error again, see below) and then remove the line again.

Fatal error: Uncaught Laminas\ModuleManager\Exception\RuntimeException: Module (NewAPI) could not be initialized. in /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php:210 Stack trace: #0 /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php(182): Laminas\ModuleManager\ModuleManager->loadModuleByName(Object(Laminas\ModuleManager\ModuleEvent)) #1 /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php(104): Laminas\ModuleManager\ModuleManager->loadModule(‘NewAPI’) #2 /vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php(331): Laminas\ModuleManager\ModuleManager->onLoadModules(Object(Laminas\ModuleManager\ModuleEvent)) #3 /vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php(180): Laminas\EventManager\EventManager->triggerListeners(Object(Laminas\ModuleManager\ModuleEvent)) #4 /vagr in /vagrant__my_app/MyApp/vendor/laminas/laminas-modulemanager/src/ModuleManager.php on line 210

I’m not completely sure if this information is necessary or even useful for finding a solution, I just thought that this is some really weird behavior that the fatal error messages differ only this little bit directly after “#4 …” ("/vagrant_" vs “/vagr”).

I have also disabled OPCache as this seems to be problematic. Still no clue, why it’s not working … :frowning:

When having the console in Firefox displayed, i get the following error when trying to create a new API:
Bildschirmfoto_2021-08-30_23-17-46

{
   "trace":[
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-admin/src/Model/ModuleResource.php",
         "line":78,
         "function":"__construct",
         "class":"Laminas\\ApiTools\\Admin\\Model\\ModuleEntity",
         "type":"->",
         "args":[
            "NewAPI"
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/AbstractResourceListener.php",
         "line":172,
         "function":"create",
         "class":"Laminas\\ApiTools\\Admin\\Model\\ModuleResource",
         "type":"->",
         "args":[
            {
               "name":"NewAPI"
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":331,
         "function":"dispatch",
         "class":"Laminas\\ApiTools\\Rest\\AbstractResourceListener",
         "type":"->",
         "args":[
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":188,
         "function":"triggerListeners",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/Resource.php",
         "line":549,
         "function":"triggerEventUntil",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/Resource.php",
         "line":242,
         "function":"triggerEvent",
         "class":"Laminas\\ApiTools\\Rest\\Resource",
         "type":"->",
         "args":[
            "create",
            {
               "data":{
                  "name":"NewAPI"
               }
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/RestController.php",
         "line":373,
         "function":"create",
         "class":"Laminas\\ApiTools\\Rest\\Resource",
         "type":"->",
         "args":[
            {
               "name":"NewAPI"
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/RestController.php",
         "line":895,
         "function":"create",
         "class":"Laminas\\ApiTools\\Rest\\RestController",
         "type":"->",
         "args":[
            {
               "name":"NewAPI"
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-mvc/src/Controller/AbstractRestfulController.php",
         "line":428,
         "function":"processPostData",
         "class":"Laminas\\ApiTools\\Rest\\RestController",
         "type":"->",
         "args":[
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas-api-tools/api-tools-rest/src/RestController.php",
         "line":335,
         "function":"onDispatch",
         "class":"Laminas\\Mvc\\Controller\\AbstractRestfulController",
         "type":"->",
         "args":[
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":331,
         "function":"onDispatch",
         "class":"Laminas\\ApiTools\\Rest\\RestController",
         "type":"->",
         "args":[
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":188,
         "function":"triggerListeners",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-mvc/src/Controller/AbstractController.php",
         "line":103,
         "function":"triggerEventUntil",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-mvc/src/Controller/AbstractRestfulController.php",
         "line":312,
         "function":"dispatch",
         "class":"Laminas\\Mvc\\Controller\\AbstractController",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-mvc/src/DispatchListener.php",
         "line":138,
         "function":"dispatch",
         "class":"Laminas\\Mvc\\Controller\\AbstractRestfulController",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":331,
         "function":"onDispatch",
         "class":"Laminas\\Mvc\\DispatchListener",
         "type":"->",
         "args":[
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-eventmanager/src/EventManager.php",
         "line":188,
         "function":"triggerListeners",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/vendor/laminas/laminas-mvc/src/Application.php",
         "line":331,
         "function":"triggerEventUntil",
         "class":"Laminas\\EventManager\\EventManager",
         "type":"->",
         "args":[
            {
               
            },
            {
               
            }
         ]
      },
      {
         "file":"/vagrant__my_app/MyApp/public/index.php",
         "line":52,
         "function":"run",
         "class":"Laminas\\Mvc\\Application",
         "type":"->",
         "args":[
            
         ]
      }
   ],
   "type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
   "title":"Internal Server Error",
   "status":500,
   "detail":"Invalid module \"NewAPI\"; no Module class exists for that module"
}

It took me days to realize that the marked solution from Laminas API Tools - unable to create new API in Admi UI - #5 by divix is what I needed to take a closer look at.

When trying to add an API in the Laminas API Tools Admin UI, I still get the same error, but now I know how to fix it (manipulate code manually) so I can still use the API Tools Admin UI:

Bildschirmfoto_2021-08-28_10-56-51 (Kopie 2)

As you can see in the screenshot above, another folder named NewAPI is automatically created under the src folder. I totally overlooked that when trying to add the new module to the autoloader in my comopser.json-file.


The following works fine for me now:

  • I do not delete the new entry ‘NewApi’ from modules.config.php
  • I am adding the new module to the autoloader in my comopser.json-file:
...
"autoload-dev": {
        "psr-4": {
            "NewAPI\\": "module/NewAPI/src/NewAPI/"
        }
    },
...

After these steps and a page refresh, my new API is available in Laminas API Tools and I can work with it.