Debugging In Zend framework 3

zend-mvc
#1

I’ve just worked through the ZF3 tutorial and it isn’t working.

The error I get is:
[Mon Mar 18 09:27:18 2019] PHP Fatal error: Uncaught Zend\ModuleManager\Exception\RuntimeException: Module (Album) could not be initialized. in /home/neily/test/zend/zf3-tutorial/vendor/zendframework/zend-modulemanager/src/ModuleManager.php:203

This is not very informative.I’ve spent some time going back through the tutorial looking for anything I’ve got wrong and also Googling this error, but I have had no luck identifying the problem.

Does anyone have any pointers?

It would be great if there was a list of common errors or a debugging guide to work through.

#2

This means that the class Album\Module couldn’t be autoloaded: this can be because the class is misplaced, because you have a typo in the file or class name, or because it is not in the Album namespace.

#3

Please check also your composer.json file:

https://docs.zendframework.com/tutorials/getting-started/modules/#autoloading

This is often overlooked or forgotten.

#4

The location is …/zf3-tutorial/module/Album/src/Module.php and the file starts with
namespace Album;

Both the namespace and the location appear correct to me?

#5

My composer.json looks fine as well.

"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
    "Album\\": "module/Album/src/"
    }

I still have no clue why this isn’t working.

#6

Is your module registered?

https://docs.zendframework.com/tutorials/getting-started/modules/#informing-the-application-about-our-new-module

#7

It would be helpful if you could deploy your project to GitHub and provide the link here.

#8

Few things to try out:

  • try composer dump-autoload (it might be that the autoloader is still the one of when you first created the project)

  • try composer dump-autoload --optimize (usually finds also classes that are in wrong locations, but in the correct path - if this works, then your path and class name mismatch)

  • is your class in namespace Album;?

#9

It is registered correctly, as far as I can see

return [
    'Zend\Form',
    'Zend\Db',
    'Zend\Router',
    'Zend\Validator',
    'Application',
    'Album',
];

composer dump-autoload doesn’t seem to help and adding --optimize doesn’t help either.
The Module class is in namespace Album.

#10

Can you please show the file contents of your Module class?

#11

Everything should be at https://github.com/SaaSNSY/Zend3

#12

Your Module.php does not start with <?php

#13

OK. That makes a difference.
Unless I’m misreading something that’s an error in the tutorial. The contents given at https://docs.zendframework.com/tutorials/getting-started/modules/#setting-up-the-album-module do not start with <?php.
I possibly should have spotted that, but I have zero PHP experience, so it wasn’t immediately obvious to me.

#14

I think there are a lot of places in the tutorial where the <?php at the start is just assumed. It is there in the .phtml code fragments, but not usually in the .php code fragments. Ideally, where it says to create a file with certain contents, it shouldn’t be omitting anything.