PHP Luminova: HMVC Application Architecture
Build modular, self-contained applications with Luminova's HMVC architecture. Organize application features into independent, reusable modules, each with its own controllers, models, views, and supporting resources.
HMVC (Hierarchical Model-View-Controller) is an architectural pattern that extends the traditional MVC (Model-View-Controller) design by introducing a modular structure. In Luminova, each module maintains its own MVC components (Models, Views, and Controllers), namespace, and directory structure, allowing modules to operate independently while remaining part of the same application.
This approach promotes separation of concerns, code reuse, and maintainability, making it easier to develop, manage, and scale complex applications.
HMVC Advantages
Modularity: Each module is self-contained and can be developed, tested, and maintained independently. This keeps the application organized and reduces complexity as it grows.
Reusability: Modules can be reused across different applications or within the same application, reducing code duplication and encouraging consistent implementations.
Maintainability: Separating application features into logical modules makes the codebase easier to understand and maintain. Changes to one module can be made with minimal impact on others.
Scalability: New features and modules can be added without disrupting the existing application structure, making it easier to expand and evolve the application over time.
Enabling HMVC Architecture
Enable the HMVC feature flag in your project's active environment configuration. If your local development environment uses a .dev.env file, set the flag there as well, provided that it is the configuration file loaded by the application.
feature.app.hmvc = enableMigrating Existing Project to HMVC
Move your controllers into the shared or named module directories shown below and update their namespaces accordingly. If you use method-based routing, update the route callbacks to reference the new HMVC controller classes.
Important Naming Convention
Use PascalCase module directories and match their case in PHP namespaces. For attribute-based HTTP routing, the first URI segment is converted to PascalCase when the router looks for a module directory. For example, /blog/posts leads discovery to app/Modules/Blog/ when that directory exists. A multiword prefix such as /news-room maps to NewsRoom.
A directory name alone does not register a route. The requested path must still match the controller’s prefix and a method route.
Key Concepts
Modules
A module is a self-contained component that encapsulates its own controllers, models, views, and other necessary resources. Modules in Luminova HMVC are designed to be reusable and scalable, often representing a distinct part of your application (e.g., user management, blog, etc.).
Controllers
Controllers in Luminova HMVC are responsible for handling requests and directing the flow of the application. Controllers can be created within either:
HTTP Controllers: Located in
app/Modules/Controllers/Http/orapp/Modules/<Module>/Controllers/Http/and handle HTTP requests.CLI Controllers: Located in
app/Modules/Controllers/Cli/orapp/Modules/<Module>/Controllers/Cli/and handle command-line interface requests.
Controller selection and template selection happen at different points. The router chooses a controller from the route, then the HTTP controller gives its class name to the view. In the usual HMVC layout, the view can infer the module from that class name.
Views
The root module uses app/Modules/Views/. A named module uses app/Modules/<Module>/Views/. The HMVC Controllers section explains how the view selects one of these directories.
Models
Models are responsible for handling the data logic of your application. Each module can have its own set of models, allowing each module to manage its own data independently. Models are typically stored in app/Modules/Models/ or app/Modules/<Module>/Models/. Import the model class you want to use, such as App\Modules\Blog\Models\Post.
HMVC Folder Structure
The Luminova HMVC structure organizes code into modules, each containing its own set of MVC components. Below is a typical file structure for a HMVC application in Luminova:
/app/
/Controllers/ (Default MVC architecture controllers)
/Http/ (HTTP controllers)
/Cli/ (CLI controllers)
/Modules/ (Modules for HMVC)
/Controllers/ (Shared HMVC Controllers)
/Http/ (Shared HTTP controllers for HMVC)
/Cli/ (Shared CLI controllers for HMVC)
/Models/ (Shared HMVC Models)
/Views/ (Shared HMVC Views)
/Blog/ (Example Module: Blog)
/Controllers/ (Controllers specific to Blog module)
/Http/ (HTTP controllers)
/Cli/ (CLI controllers)
/Models/ (Models specific to Blog module)
/Views/ (Views specific to Blog module)
/User/ (Example Module: User)
/Controllers/ (Controllers specific to User module)
/Http/ (HTTP controllers)
/Cli/ (CLI controllers)
/Models/ (Models specific to User module)
/Views/ (Views specific to User module)Directory Structure Overview
- Modules/: Root container for all application modules, including the default (root) module.
- Module/: Each module represents a self-contained feature or section of the application.
- Controllers/: Handles incoming requests and business logic specific to the module.
- Models/: Defines data structures and database interactions used by the module.
- Views/: Contains the presentation layer, HTML or template files rendered by the module.
- Module/: Each module represents a self-contained feature or section of the application.
Note
By default, the kernel returns one
App\Applicationfor the request. A custom kernel can choose a different application class throughgetApplication().
Namespaces
In Luminova's HMVC architecture, proper namespace registration is essential for organizing and autoloading your application components. Namespace patterns vary slightly depending on whether you're using standard MVC or modular HMVC.
MVC Namespace Conventions
\App\Controllers\Http\For standard HTTP controllers in a non-modular structure.\App\Controllers\Cli\For command-line controllers (CLI) in a non-modular structure.
HMVC Namespace Conventions
\App\Modules\<ModuleName>\Controllers\Http\For HTTP controllers within a specific HMVC module.\App\Modules\<ModuleName>\Controllers\Cli\For CLI controllers within a specific HMVC module.
Namespace Registration
Register module controller namespaces in your application's Application::onCreate() method using the router's addNamespace() method. Each registered namespace becomes a lookup root for resolving module controllers.
Example
// /app/Application.php
namespace App;
use Luminova\Foundation\Core\AbstractApplication;
class Application extends AbstractApplication
{
/**
* Boot and register controller namespaces.
*/
protected function onCreate(): void
{
// Register module controller namespaces.
$this->router
->addNamespace('\\App\\Modules\\Blog\\Controllers\\')
->addNamespace('\\App\\Modules\\User\\Controllers\\');
}
}During route resolution, Luminova appends the appropriate context namespace to each registered root: Http for web requests and Cli for CLI commands. It then checks the namespaces in registration order until a matching controller is found.
If multiple registered namespaces contain controllers with the same basename in the same context, the first matching controller takes precedence. For method-based routes, use a fully qualified callback to identify the intended controller explicitly.
Note
The MVC and HMVC root namespaces for both HTTP and CLI are predefined by Luminova. Register additional module controller roots when callbacks use short controller names. This also applies to attribute routing because the attribute compiler registers callbacks by short class name.
HMVC Routing and Controller Resolution
Luminova supports two controller resolution modes, controlled by the feature.route.attributes configuration option.
When enabled, HTTP discovery uses the first URI segment to look for a module directory, then matches a controller’s #[Prefix] and its method’s #[Route]. CLI discovery matches the first command argument against #[Group], then registers the controller’s #[Command] methods.
When disabled, Luminova uses method-based routing. The router loads the configured route files from /routes/*.php for the active request context and dispatches the explicitly registered callbacks.
Register routes in the appropriate route file and reference controllers using either their fully qualified class names or short names, provided their controller namespace roots have been registered with the router.
Example
With attribute routing disabled, register a Blog controller action in the web route file:
// /routes/web.php
use Luminova\Routing\Router;
use App\Modules\Blog\Controllers\Http\BlogController;
Router::get('/blog', BlogController::class . '::index');This explicitly maps the /blog route to the index() method of the module's BlogController.
See HTTP route attributes, CLI command attributes, and method-based routing.
HMVC Controllers
The HTTP controller passes its concrete class name to View. On the first template lookup, the view reads the first namespace segment after App\Modules\. Blog selects app/Modules/Blog/Views/. A root controller under App\Modules\Controllers\Http selects app/Modules/Views/. The URI, route prefix, and CLI group do not determine the template directory.
Example: Blog Controller
// /app/Modules/Blog/Controllers/Http/BlogController.php
namespace App\Modules\Blog\Controllers\Http;
use Luminova\Base\Http\AbstractController;
use Luminova\Attributes\Route;
use Luminova\Attributes\Prefix;
use App\Errors\Controllers\AppError;
#[Prefix(
pattern: '/blog/(:root)',
onError: [AppError::class, 'onTrigger']
)]
class BlogController extends AbstractController
{
#[Route('/blog', methods: ['GET'])]
public function index(): int
{
// Logic to display the blog index page
return $this->view('blogs');
}
}With the PHP template engine, this resolves blogs to app/Modules/Blog/Views/blogs.php. The view uses the configured engine’s extension. A missing template does not trigger a search in another module’s view directory.
Override the template module
setModule() is optional. Use it before selecting a template when an action should render from another named module. Put the call in onCreate() to apply the override to all actions in that controller.
Example:
protected function onCreate(): void
{
$this->tpl->setModule('Shared');
}This selects app/Modules/Shared/Views/blogs.php with the PHP engine. It does not register a controller namespace or alter routing. Use View::setFolder() for a subdirectory inside the selected module’s Views/ directory.
Note:The
setModule()method rejects both an empty value and'/', so use the root controller layout for automatic root view selection.
If you create a View object directly outside an HTTP Controller, supply the controller class with View::setController() before the first template lookup, or set a named module explicitly with setModule(). The view keeps the resolved module on that instance, so use a fresh view or an explicit override when switching between controllers.
HMVC CLI Controllers
CLI controllers in Luminova allow you to define commands that can be executed through the command line interface. These controllers are typically used for background tasks, maintenance commands, or other non-HTTP operations.
Example: Blog CLI Controller
// /app/Modules/Blog/Controllers/Cli/BlogController.php
namespace App\Modules\Blog\Controllers\Cli;
use Luminova\Attributes\Group;
use Luminova\Attributes\Command;
use Luminova\Base\Cli\AbstractController;
#[Group(name: 'blog')]
class BlogController extends AbstractController
{
protected string $group = 'blog';
protected string $name = 'blog-command';
/**
* @var array<int|string,string>|string
*/
protected array|string $usages = [
'php public/index.php blog sync',
];
#[Command('sync', group: 'blog')]
public function sync(): int
{
// Logic to handle blog synchronization from the CLI
return STATUS_SUCCESS;
}
public function help(array $helps): int
{
return STATUS_SUCCESS;
}
}Note:
In CLI controllers,
Commandattributes define specific command names, and theGroupattribute is used to group related commands together. Run this example withphp public/index.php blog sync. Register theBlogcontroller root in the application as shown above.