Modules
A module is a folder that holds everything for one part of the site: controllers, view scripts, configuration. A blog, a shop, a user account area — each in its own module. The code does not get mixed up, and a finished module can be moved to another project.
Laminas has no command for creating a module — the folders and files are created by hand. There are five steps.
Step 1. Folders
Create this structure inside the module folder:
module/Blog/
├── config/
│ └── module.config.php
├── src/
│ └── Module.php
└── view/The module folder name starts with a capital letter: it matches the PHP namespace.
Step 2. The module class
This class tells Laminas that the module exists and where its configuration is. Create the file module/Blog/src/Module.php:
// module/Blog/src/Module.php
namespace Blog;
class Module
{
public function getConfig(): array
{
return include __DIR__ . '/../config/module.config.php';
}
}The getConfig() method returns the array from the module's config file. Laminas calls it on startup.
Step 3. The config file
Create the file module/Blog/config/module.config.php. For now it holds an empty array:
// module/Blog/config/module.config.php
namespace Blog;
return [];The next lessons add routes, controllers and view script paths here. The namespace Blog; line lets you write the module's class names in a shorter form.
Step 4. Class loading
Composer must know which folder holds the classes whose names start with Blog\. Open composer.json in the project root and add a line to the autoload section:
{
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Blog\\": "module/Blog/src/"
}
}
}Leave the other sections of the file as they are. Then refresh the class loader:
composer dump-autoloadStep 5. Enabling
Add the module name to the end of the list in config/modules.config.php:
// config/modules.config.php
return [
'Laminas\Router',
'Laminas\Validator',
'Application',
'Blog',
];Modules are loaded in the order of the list. Open http://localhost:8080: if the home page opens without errors, the module is enabled. It has no pages of its own yet — they appear in the routing lesson.
Common mistake
If you skip step 4 or forget composer dump-autoload, the site shows the error Module (Blog) could not be initialized. It means the module name is in the list but the Blog\Module class was not found. Check the line in composer.json, the folder name and the namespace in Module.php — the letter case must match.