In this article, we are going to see how to create custom artisan commands in bagisto.
What is Artisan Command?
Artisan is a command line tool in Laravel to interact with the application. Artisan helps in lot way to create application classes, databases, migrations, run server or view application list.
As we know, Laravel provides artisan commands to interact with applications. You really wonder how the artisan command works. You can also create your custom artisan command in Bagisto.
Create Custom Artisan Command in Bagisto
You can create your own custom artisan command, which will execute blocks of code as per requirements. This includes creating caching, running cronjobs, sending mails etc.
Steps to create the command:-
Step 1: Create an artisan command file
Create a New Command directory inside the Console folder of the package, and make a custom command class inside the command folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace Webkul\Core\Console\Commands; use Illuminate\Console\Command; class BagistoVersion extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'bagisto:version'; /** * The console command description. * * @var string */ protected $description = 'Displays current version of Bagisto installed'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $this->comment('v' . core()->version()); } } |
Step 2: Customize the command class file as per your requirement
After creating the command class file at packages\Webkul\Core\Console\Commands\BagistoVersion.php
$signature property of the command class will be the command that you will execute into the terminal.
In our case, set $signature to bagisto:version
1 |
protected $signature = 'bagisto:version'; |
Next, set up the $description as per you wish,This will tell you what the command does.
1 |
protected $description = 'Displays current version of Bagisto installed'; |
Next, in the handle method which is the code block function executed when the command run, perform whatever action you intend it to perform. In this example, we are displaying the bagisto version.
1 2 3 4 |
public function handle() { $this->comment('v' . core()->version()); } |
Step 3: Register Your Command in your Package’s Service-Provider
Register all the custom commands in the register() method of the service provider
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function register(): void { if ($this->app->runningInConsole()) { $this->commands([ \Webkul\Core\Console\Commands\BagistoPublish::class, \Webkul\Core\Console\Commands\BagistoVersion::class, \Webkul\Core\Console\Commands\Install::class, \Webkul\Core\Console\Commands\ExchangeRateUpdate::class, \Webkul\Core\Console\Commands\BookingCron::class, \Webkul\Core\Console\Commands\InvoiceOverdueCron::class ]); } } |
Finally, In the terminal, run the command to see the desired result.
1 |
php artisan bagisto:version |
The output of the command in the terminal.
1 2 3 |
php artisan bagisto:version v1.4.3 |
So, that was much about the article “ How to Create Custom Artisan Commands in Bagisto ”. Also for any queries or doubts reach out to us at support@webkul.com. You can also raise a ticket at our HelpDesk System.