Artisan Command
The very basic installment that laravel provide is in Kernel.php located at app/Console. To use this console command we can open our command prompt inside our project directory and then write
php artisan make:command QuizStart --command quiz:startt
php artisan make: command - this create a command class inside Console/Commands folder
--command - will assign our command signature as "quiz:start"
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class QuizStart extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'quiz:start'; /** * 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() { // } }
However , we need to display some information if we write the command. The handle function will do the code that correspond to our command. So here we are gonna do some construct several text to require confirmation from user.
public function handle()
{
if ($this->confirm('Are you a human? [yes|no]'))
{
$this->info('Authorization Success!');
}
}
However , the command are yet cannot be run since we didnt put our command class inside Kernel.php. To solve this problem, we need to assign or write our command directory inside $command property in Kernel.php.
protected $commands = [
'App\Console\Commands\MyCommand',
'App\Console\Commands\QuizStart'
];
References :
- http://ourcodeworld.com/articles/read/248/how-to-create-a-custom-console-command-artisan-for-laravel-5-3
- https://laravel.com/docs/5.4/artisan#writing-commands
No comments:
Post a Comment