Architecture
Data types
The core architecture of MV relies on data types for models and forms. These include both common programming types (such as string, integer, etc.) and special types designed for easier handling (files, images, image arrays, groups).
All data types correspond to fields in an SQL table, except for the 'many-to-many' type, which creates a linking table, and the 'many-to-one' type, which represents a reverse count of records by a foreign key. Types are assigned special attributes that define unique settings for each field.
To create a field in a model or form, you need to populate an array with 3 required parameters and one optional:
- Text label for the field.
- Data type.
- Field name in the SQL table.
- An optional array of additional parameters.
['Active', 'bool', 'active'],
['Name', 'char', 'name', ['required' => true]],
['Album', 'enum', 'album', ['foreign_key' => 'Albums', 'empty_value' => true]],
['Rating', 'float', 'rating'],
['Position', 'order', 'order'],
['Image', 'image', 'image', ['required' => true]]
Models and templates
The main components of MV are models – classes for managing data in SQL tables – and templates, which are PHP files for generating HTML code. A model consists of multiple data types that interact as objects within the model and with other models. The model's fields automatically generate an administrative interface.
class News extends Model
{
protected $name = 'News';
protected $model_elements = [
['Active', 'bool', 'active', ['on_create' => true]],
['Date', 'date', 'date', ['required' => true]],
['Name', 'char', 'name', ['required' => true]],
['Content', 'text', 'content', ['rich_text' => true]],
['Comments', 'many_to_one', 'comments', ['related_model' => 'Comments']]
];
public function display()
{
$rows = $this -> select(['active' => 1, 'order->desc' => 'date']);
$html = '';
foreach($rows as $row)
{
$html .= '<h2>'.$row['name'].'</h2>';
$html .= '<p>Date: <span>'.I18n::formatDate($row['date']).'</span></p>';
$html .= '<p>'.$row['content'].'</p>';
}
return $html;
}
}
PHP serves as the templating engine, with template files located in a dedicated folder and loaded based on the URL processed by the router. Templates retrieve necessary records from SQL tables and call model methods to display data.
<div id='content'>
<h1><? echo $record -> name; ?></h1>
<div class='date'><? echo I18n::formatDate($record -> date); ?></div>
<? echo $record -> content; ?>
</div>
Routing
Template (view) selection is achieved through URL-based routing. The requested URL is divided into segments and analyzed by the router, which then selects the corresponding template. For performance optimization, URLs are matched against predefined patterns without regular expressions, as described in the General template principles section.
A template is always loaded, even if no match is found; a default fallback template is loaded, from which a 404 error can be triggered if necessary.
//File config/routes.php
'/' => 'view-index.php',
'e404' => 'view-404.php',
'fallback' => 'view-default.php',
'/contacts' => 'view-contacts.php',
'/user/login' => 'client/view-login.php',
'/news/*' => 'view-news.php',
'/payment/success/?' => 'view-pay-success.php'
Forms
Forms are generated based on data types. Forms can also be created from models, utilizing an existing field structure. MV provides tools for form field validation, field rendering and error display, message assembly, and quick creation of records in SQL tables based on form data.
//Form based on model
$form = new Form('News');
//Form based on array of fields
$fields = [
['Name', 'char', 'name', ['required' => true]],
['Email', 'email', 'email', ['required' => true]],
['Phone', 'phone', 'phone'],
['Question', 'text', 'question', ['required' => true]]
];
$form = new Form($fields);
<form method='post' action='<? echo $mv -> root_path; ?>form'>
<? echo $form -> display(); ?>
<button>Submit</button>
</form>
Admin panel
MV includes an admin panel that automatically generates an interface for managing models. Each active model has its own section in the admin panel, allowing users to create, edit, and delete records.
The following components are part of the admin panel:
- lists of all model records
- record management (CRUD)
- deletion with recovery options (trash bin)
- record search and filtering across all fields
- bulk record operations
- change history for all fields of each record
- administrators with customizable permissions
- administrator operation history
- file manager