Registering Commands
The load
method call in your console kernel's commands
method, all commands within the app/Console/Commands
directory will automatically be registered with Artisan. In fact, you are free to make additional calls to the load
method to scan other directories for Artisan commands:
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__.'/MoreCommands');
// ...
}
You may also manually register commands by adding its class name to the $commands
property of your app/Console/Kernel.php
file. When Artisan boots, all the commands listed in this property will be resolved by the service container and registered with Artisan:
protected $commands = [
Commands\SendEmails::class
];
Example(1)
1.Create new commands using below commands in your project
php artisan make:command demo
app/Console/Commands
directory.app/Console/Commands/demo.php file and that looks like below code
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class demo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'demo:command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}
Inside this class you will find protected $signature
and protected $description
variables, it represents name and discription of your command which will be used to describe your command.
after creating command you can register your command inside app/Console/Kernel.php
class where you will find commands
property.
so you can add your command inside the $command array like :
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\demo::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
and then use command in command prompt
php artisan demo:command