Retrieving Plugin Versions in OctoberCMS 3 using VersionManager and PluginVersion
OctoberCMS 3 offers two classes to retrieve the version number of a plugin: VersionManager and PluginVersion.
VersionManager
The getLatestVersion()
method of \System\Classes\VersionManager
retrieves the current version of a plugin from the associated version.yaml
file. This file is a fundamental requirement and should be included in every plugin since at least OctoberCMS v2. With this class, you can access the latest plugin version without the need for the plugin to be installed and regardless of whether it is activated or not. However, keep in mind that when using the VersionManager
, the version specified in the version.yaml
file may differ from the installed version stored in the database.
Example
The following code returns the version number of the "RainLab.User" plugin as a string or 0
if the plugin or the version.yaml
file does not exist.
$versionManager = \System\Classes\VersionManager::instance();
echo $versionManager->getLatestVersion('RainLab.User');
PluginVersion
The class \System\Models\PluginVersion
is, as the namespace suggests, an Eloquent model that maps to the native system_plugin_versions
table, thus we can retrieve the latest installed version number from the respective column. This class therefore requires that the plugin is installed, but it also allows for a direct check to see if the plugin is currently activated as well.
Example
The following code demonstrates the use of the PluginVersion
class in a typical Eloquent manner. With an additional where
clause, like where('is_disabled', 0)
, you can further restrict the selection to active plugins too.
$pluginVersion = `\System\Models\PluginVersion::where('code', 'RainLab.User')->first();
echo $pluginVersion ? $pluginVersion->version : 0;
Plugin Manager
By the way, using the \System\Classes\PluginManager
class, you can also quickly check whether a plugin exists or not. Similar to VersionManager
, this class only verifies if the plugin directory (and necessary files) exists, without checking whether the plugin is installed or activated.
Example
The following code checks whether the RainLab.User
plugin exists:
echo \System\Classes\PluginManager::instance()->hasPlugin('RainLab.User');
There are no comments yet
Be the first one to comment