Create custom CLI command
Using hostware modules you can easiliy create cli commands which can be executed with php artisan or as a scheduled task. This also has the benefit that debugging scheduled tasks is as easy as executing the command you created.
hostware uses the default commands of the Laravel Framework. Take a look at the official documentation!
Creating a command
A command is a class file which is stored in the directory /Console/COMMANDNAME.php and looks like the following:
Console/CheckInstallingProducts.php
<?php
namespace custom\modules\DashservVps\Console;
class CheckInstallingProducts extends Command
{
// The unique name of the command which will be used for execution
protected $name = 'dashservvps:checkInstallingProducts';
// A small description of what the command will do
protected $description = 'Checks pending hosting products for installation';
public function __construct()
{
parent::__construct();
}
// This function will execute once the command will be executed. You can perform anything inside!
public function handle()
{
// YOUR CODE
}
}The file and classname should be identical. You can now execute the command via the $name you have defined inside of it. Please use your module name as a prefix and after the : you can use the name of your class as name of the command.
Register the command
To register the command of your module, please open the ServiceProvider class of your module in the directory /Providers/ (In this case /Providers/DashservVpsServiceProvider.php)
This file may look like the following per default:
To register the command, add the boot() function and the register the class of your command. The file should look like this afterwards:
Executing the command
You can now execute the command in your hostware root directory calling php artisan COMMANDNAME. By just executing php artisan you get an overview of all registered comamnds.
In this case, you would need to execute php artisan dashservvps:checkInstallingProducts which would then execute the code inside of the handle() function.
Execute as scheduled task
Learn here how to execute the command as a scheduled task in predefined intervals.