Writing your own custom Laravel Artisan Command

Sachin Kiranti
4 min readSep 30, 2018

--

We have been using many artisan commands in our day to day work . Laravel provides many artisan commands to make our life easier . From creating model to controller , request and many other with options for generating related materials like

php artisan make:model Hello -mcr

Above commands will generate model, migration, resource controller for your model Hello .

Generating model, migration and resource controller

Easy-peasy right ? 🙂 With a line of command we have our model, migration and resource controller ready . There are many other helpful commands which Laravel provides out of the box for you . You can list out all the commands using :

php artisan list

In addition to that, you can found many plugins out there to extend your artisan commands experience 🙂.

Today , we will be creating a simple custom artisan command which will clean up our cache, route, view with a line of command . We do have all the mention features already in laravel , we will be utilizing that in our artisan command . Let’s get started . 🙂

Getting Started

First, let’s hit a command to generate the blueprint for our artisan command . Enter the below command on your terminal .

php artisan make:command CleanUpCommand

Now , you will find your command on app/console/commands/ . We will start coding our artisan command right away but first please do read Laravel documentation about creating commands . I won’t be summarizing how exactly does artisan command works. This is all well documented on Laravel official website .

We will be using bunch of already made commands like :

  • php artisan view:clear
  • php artisan route:clear
  • php artisan cache:clear
  • php artisan config:clear
  • php artisan clear-compiled

First let’s declare a protected variable with arrays of commands .

/*** List of artisan commands* @var array*/protected $commands = [ 'view:clear', 'cache:clear', 'config:clear', 'route:clear', 'clear-compiled' ];

After that, let’s inject our composer on our constructor .

/*** The Composer instance.* @var \Illuminate\Support\Composer*/protected $composer;/*** Create a new command instance.* @param Composer $composer* @param Artisan $artisan*/public function __construct(Composer $composer){parent::__construct();$this->composer = $composer;}

After initializing the composer , let’s start the main functionality we were talking about .

/*** Execute the console command.* @return void*/public function handle(){// First $this->composer->dumpAutoloads();$this->composer->dumpOptimized();// Second$outputs = [];foreach ($this->commands as $command) :$this->call($command);endforeach;// Thirdif (function_exists('exec')) :exec('truncate -s 0 storage/logs/laravel.log', $output, $result);if (!$result) {$outputs[] = 'Log cleared successfully!';} else {$outputs[] = 'Log clear failed!';}else :$outputs[] = 'Log clear command cannot be init! exec is disabled!';endif;$this->info(implode('', $outputs));$this->info('All done successfully ! Love kiranti :) ');}

We have written complete function for our command . The first two line of code will do the job like :

composer dumpautoload

In second line of code , we are just calling the artisan commands for each item from our array from the class .

php artisan view:clear

and so on .

In the third part of code , we are using exec() function to execute the external command which will truncate our log file .

At the end , we are imploding the output (messages) . The final thing to do is give a signature and description for our command .

/*** The name and signature of the console command.* @var string*/protected $signature = 'kiranti:cleanup';/*** The console command description.* @var string*/protected $description = 'Clean up all views, cache and dump composer';

Our command is ready . All we need is to register our commands on App\Console\kernel.php file . We just need to add our command in protected $commands array .

/*** The Artisan commands provided by your application.* @var array*/protected $commands = [CleanUpCommand::class];

The final code :

The final code

Finally , we have our command ready 😍. If you run php artisan list , we will see our command listed there .

Our command on the artisan list

Let’s run our command 🙂 and test it out .

php artisan kiranti:cleanup

php artisan kiranti:cleanup

Conclusion

This is just a simple demonstration for a command but you can play around and make something useful . We have covered a lot of topic in this article . Hope this help you understand commands and let you create your own . Happy coding 😍

--

--