Laravel - Craft Console

Laravel - Craft Console

The Laravel framework provides three main tools for command-line interaction, namely Artisan, Ticker, and REPL . This chapter details Artisan.

Introduction to Artisan

Artisan is a command line interface often used in Laravel and includes a set of useful commands for web application development.

example

Here is a list of several commands in Artisan along with their respective functions −

To start a Laravel project

php artisan serve

Enable caching mechanism

php artisan route:cache

To view a list of available commands supported by Artisan

php artisan list

To view help for any command and view available options and arguments

php artisan help serve

The following screenshot shows the output of the above commands −

Command Writing

In addition to the commands listed in Artisan, the user can also create a custom command that can be used in a web application. Note that the commands are stored in the app/console/commands directory .

The default command to create a custom command is shown below −

php artisan make:console <name-of-command>

Once you type the above command, you will be able to see the output as shown in the screenshot given below −

The file created for the DefaultCommand is called DefaultCommand.php and is shown below:

<? php

namespace App \Console\Commands ; use Illuminate \Console\Command ; 
 

class DefaultCommand extends Command { /**   
   
      * The name and signature of the console command.
      *
      * @varstring
   */
   
   protected $signature = 'command:name' ; 
   
   /**
      * The console 
command description.
      *
      * @varstring
   */
   
   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 () { // } }  
      
   

This file contains the signature and description for the user-defined command. The public function named handle executes the functionality when the command is executed. These commands are registered in the Kernel.php file in the same directory.

You can also create a task schedule for a user defined command as shown in the following code −

<? 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.
      *
      * @vararray
   */
   
   protected $commands = [ // Commands\Inspire::class, Commands \DefaultCommand :: class ]; 
      
      
   
   
   /**
      * Define the application's command schedule.
      *
* @param \Illuminate\Console\Scheduling\Schedule $schedule
      * @return void
   */
   
   protected function schedule ( Schedule $schedule ) { // $schedule->command('inspire') // ->hourly(); } }  
      
      
   

Note that the task schedule for this command is defined in a function named schedule , which includes a parameter for task scheduling that takes an hourly parameter.

The commands are registered in the commands array, which includes the path and name of the commands.

Once a command is registered, it is listed in the Artisan commands. The values ​​included in the label and description section will be displayed when the help attribute of the specified command is invoked.

Let's see how to view the attributes of our DefaultCommand command . You have to use the command as shown below −