Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
Zend Framework is modular by design, which means your application can be organized into multiple self-contained modules, each with its own controllers, views, models, and configuration.
A Module in Zend is like a mini-application. It can contain:
Modules make your app scalable, maintainable, and reusable.
Each module should follow a consistent folder structure. Example:
module/ └── Blog/ ├── config/ │ └── module.config.php ├── src/ │ └── Module.php │ └── Controller/ │ └── IndexController.php ├── view/ │ └── blog/ │ └── index/ │ └── index.phtml
Create a new folder under /module:
mkdir module\Blog\src\Controller mkdir module\Blog\view\blog\index mkdir module\Blog\config
Module.php in (module\Blog\src\Module.php)<?php
declare(strict_types=1);
namespace Blog;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}
module.config.php in (module\Blog\config\module.config.php)<?php declare(strict_types=1); namespace Blog; use Laminas\Router\Http\Literal; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'router' => [ 'routes' => [ 'blog' => [ 'type' => Literal::class, 'options' => [ 'route' => '/blog', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'index', ], ], ], ], ], 'controllers' => [ 'factories' => [ Controller\IndexController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'template_path_stack' => [ 'blog' => __DIR__ . '/../view', ], ], ];
IndexController.php in (module\Blog\src\Controller\IndexController.php)<?php
declare(strict_types=1);
namespace Blog\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel(['message' => 'Welcome to Blog Module!']);
}
}
<h2><?= $this->escapeHtml($message) ?></h2>
Add Blog to the config/modules.config.php file:
return [ 'Laminas\Router', 'Laminas\Validator', 'Application', 'Blog', // ✅ Add this line ];
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Blog\\": "module/Blog/src/"
}
},
composer dump-autoload
Visit: http://localhost/blog
<h2>Welcome to Blog Module!</h2>