Creating a new Artisan Command In Lumen

Just playing around with the Artisan Command. So, i decided to write my own key generator. So i got this KeyGenerateCommand class here. So, for those of you who may want to have your own custom command, follow the steps below.

Create the Command

So we are going to create a new command app/Console/Commands/KeyGenerateCommand.php


<?php namespace App\Console\Commands;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class KeyGenerateCommand extends Command
{
     /**
     * The console command name.
     *
     * @var string
     */
     protected $name = 'key:generate';

     /**
     * The console command description.
     *
     * @var string
     */
     protected $description = "My application key";

     /**
     * Execute the console command.
     *
     * @return void
     */
     public function fire()
     {
         $key = $this->getRandomKey();

         if ($this->option('show'))
         {
             return $this->line('<comment>'.$key.'</comment>');
         }

         $path = base_path('.env');

         if (file_exists($path))
         {
             file_put_contents($path, str_replace($this->laravel['config']['app.key'], $key, file_get_contents($path)));
         }

         $this->laravel['config']['app.key'] = $key;

         $this->info("Application key [$key] set successfully.");
     }

     /**
     * Generate a random key for the application.
     *
     * @return string
     */
     protected function getRandomKey()
     {
         return Str::random(32);
     }

     /**
     * Get the console command options.
     *
     * @return array
     */
     protected function getOptions()
     {
         return array(array('show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'),);
     }
}

Create a New Service Provider

As good programmers, we should get in the habit of writing clean code. As tempting as it may be to bind your new command in the default AppServiceProvider.php class; we should always endeavour to create our own serviceProviders to house all our Commands. So we would create a new service provider: app/Providers/KeyGenerateServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Console\Commands\KeyGenerateCommand;

class KeyGenerateServiceProvider extends ServiceProvider
{

     /**
     * Register any application services.
     *
     * @return void
     */
     public function register()
     {
         $this->app->singleton('command.key.generate', function()
         {
           return new KeyGenerateCommand;
         });

         $this->commands(
             'command.key.generate'
         );
     }
}

Register the Service Provider

Finally, you’ll have to register the service provider that we just created above for the purposes of binding your new command service to the container.bootstrap/app.php


<?php
/*
|------------------------------------
| Register Service Providers
|------------------------------------
|
| Here we will register all of the application's
| service providers which are used to bind
| services into the container. Service providers
| are totally optional, so you are not required
| to uncomment this line.}
|
*/

// $app->register('App\Providers\AppServiceProvider');
$app->register('App\Providers\KeyGenerateServiceProvider');

Since we’ve added new files; Run composer dumpautoload to add them to the classmap.
That’s it!
Use php artisan list to list all available commands
And fire your new command with php artisan key:generate