Blog / Make your own Artisan script

Image used for article Make your own Artisan script

Make your own Artisan script

TL;DR : During the development of my Flintable Project, I wanted to customize the Artisan script. Here's how to use the php artisan command line without using the word artisan.

You will find the source code via this Github Repository. Learn more on Capsules or X.

Every Laravel artisan will recognize the php artisan inspire command. But not everyone might realize that this command can be customized.

The unspeakable secret: modifying the artisan file at the root of the project.

mv ./artisan ./is-dead

Incredible! The command php is-dead inspire will from now on work just as well as the initial php artisan inspire command.

We could put a stop to this drama right here, you might say, but we will go further.

Some of you may already know, but it's also possible to call the Artisan command via the Facade\Artisan as shown below in the console.php routes file

Artisan::command( 'inspire', function () {
    $this->comment( Inspiring::quote() );
})->purpose( 'Display an inspiring quote' );

All that would then be needed is to copy the /vendor/laravel/framework/Illuminate/Support/Facades/Artisan.php file.


namespace Illuminate\Support\Facades;

use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;

class Artisan extends Facade
    protected static function getFacadeAccessor()
        return ConsoleKernelContract::class;

And then modify the relevant information in a new file [ and folder ] in app/Facades/IsDead.php.


namespace App\Facades;

use Illuminate\Support\Facades\Facade;
use Illuminate\Contracts\Console\Kernel;

class IsDead extends Facade
    protected static function getFacadeAccessor() : string
        return Kernel::class;

To then be able to use it in your code, you need to inject the alias into the list of aliases in the configuration file config/app.php.

'aliases' => Facade::defaultAliases()->merge( [
        'IsDead' => App\Facades\IsDead::class
    ] )->toArray(),

Let's modify the inspire method in routes/console.php so we can read it in tinker.

use Illuminate\Foundation\Inspiring;
use App\\Facades\IsDead;

IsDead::command( 'inspire', function () {
   echo Inspiring::quotes()->random();
})->purpose( 'Display an inspiring quote' );

Now, all that's left is to use it in php is-dead tinker.

IsDead::call( 'inspire' );

In order for the project dependencies to be installed properly, it is necessary to replace php artisan with php is-dead in the scripts section of the composer.json file.

  "scripts": {
    "post-autoload-dump": [
      "@php is-dead package:discover --ansi"
    "post-update-cmd": [
      "@php is-dead vendor:publish --tag=laravel-assets --ansi --force"
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    "post-create-project-cmd": [
      "@php is-dead key:generate --ansi"

Glad this helped.


X IconGithub Icon