Plugins
Plugins in MV are classes for performing tasks that do not require managing a table in the database, as is the case with models.
Plugins can be instantiated in the $mv object immediately upon its creation, and not like models using the lazy load principle.
Basic rules
- The plugin class must inherit from the Plugin class.
- The file must have a name like plugin_name.plugin.php and be located in the plugins folder in the root of the project.
- To activate a plugin, its class must be specified in the config/plugins.php file.
- The plugin object is available in the $mv -> PluginName object.
- The plugin is instantiated before the template is selected and included, immediately upon the creation of the $mv object, if the $auto_start property value is set.
//File plugins/cart.plugin.php
class Cart extends Plugin
{
//If the plugin needs to be started before the template file is included
protected $auto_start = true;
private $items = [];
private $products_model;
public function __construct()
{
//Unlike models, the constructor can be supplemented
parent :: __construct();
//Product model object
$this -> products_model = new Products();
}
public function addItemToCart($id, $quantity)
{
...
}
}
//Plugin activation in the config/plugins.php file
$mvActivePlugins = [..., 'Cart'];
//Example of using the plugin in a template file
$mv -> cart -> addItemToCart(56, 2);
Previous section
Authorization