Skip to content

MVC Pattern in XOOPS

XMF Required 4.0.x Native

The Model-View-Controller (MVC) pattern is a fundamental architectural pattern for separating concerns in XOOPS modules. This pattern divides an application into three interconnected components.

The Model represents the data and business logic of your application. It:

  • Manages data persistence
  • Implements business rules
  • Validates data
  • Communicates with the database
  • Is independent of the UI

The View is responsible for presenting data to the user. It:

  • Renders HTML templates
  • Displays model data
  • Handles user interface presentation
  • Sends user actions to the controller
  • Should contain minimal logic

The Controller handles user interactions and coordinates between Model and View. It:

  • Receives user requests
  • Processes input data
  • Calls model methods
  • Selects appropriate views
  • Manages application flow

In XOOPS, the MVC pattern is implemented using handlers and templates with the Smarty engine providing template support.

<?php
class UserModel
{
private $db;
public function getUserById($id)
{
// Database query implementation
}
public function createUser($data)
{
// Create user implementation
}
}
?>
<?php
class UserController
{
private $model;
public function listAction()
{
$users = $this->model->getAllUsers();
return ['users' => $users];
}
}
?>
{foreach from=$users item=user}
<div>{$user.username|escape}</div>
{/foreach}
  • Keep business logic in Models
  • Keep presentation in Views
  • Keep routing/coordination in Controllers
  • Don’t mix concerns between layers
  • Validate all input at the Controller level

See also:


Tags: #mvc #patterns #architecture #module-development #design-patterns