I found that when adding console commands to my Laravel install, I got fed up of typing the same long commands everytime, for tasks we do regularly at work.
Now the most common solution to this would be to create bash aliases on my local machine:
alias short = 'php artisan this-is-a-long-command'
But that then means I have logic in 2 places, making it harder to maintain, and I would have to share it with my team anytime I updated it or vice versa.
The Solution
Enter console command aliases. Now this isn’t something new, Symfony has supported console command aliases for years, and its not in Laravel’s documentation, so unless your familar with Symfony, you won’t know it exists.
Lets start with a basic command, I’ve omitted the docblocks so its easier to read:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SayHelloCommand extends Command
{
protected $signature = 'say:hello {name : Name}';
protected $description = 'Say hello to someone';
public function handle()
{
$this->info('Hello ' .$this->argument('name'));
return 0;
}
}
Now running php artisan say:hello
isn’t really a long command, but bear with me.
To add an alias, or aliases, we need to override the configure
method that belongs to Symfony. Here you can do other configuration for your command, not just to add aliases:
protected function configure()
{
$this->setAliases([
's:h',
]);
parent::configure();
}
As you can see, setAliases
accepts an array, meaning you can set mutliple aliases for your command. I don’t think I’ve needed multiple, but if you do, go for it.
Now our command looks like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SayHelloCommand extends Command
{
protected $signature = 'say:hello {name : Name}';
protected $description = 'Say hello to someone';
protected function configure()
{
$this->setAliases([
$this->alias,
]);
parent::configure();
}
public function handle()
{
$this->info('Hello ' .$this->argument('name'));
return 0;
}
}
Which means we can call either
php artisan say:hello Tom
or
php artisan s:h Tom
Pros
- It adds aliases directly to the command so they can easily be maintained.
- Other developers get access to the same aliases. I think this is the key here.
Cons
- It doesn’t look very Laravel like does it?
Solution 2
I created a package to make this setup alot nicer. Which you can find here. It also supports interactive commands, but I’ll leave that for another post.
It supports Laravel 9 currently. Support for the upcoming Laravel 10 is due in the near future.
First we need to install the package:
composer require smashed-egg/laravel-console
Next we need to update the command, so it now extends:
SmashedEgg\LaravelConsole\Command
instead of
Illuminate\Console\Command
We extend this in our class, giving you the same Laravel functionality.
Next we can remove this:
protected function configure()
{
$this->setAliases([
$this->alias,
]);
parent::configure();
}
And instead add the following below the signature:
protected $alias = 's:h';
Making the command look like this:
<?php
namespace App\Console\Commands;
use SmashedEgg\LaravelConsole\Command;
class SayHelloCommand extends Command
{
protected $signature = 'say:hello {name : Name}';
protected $alias = 's:h';
protected $description = 'Say hello to someone';
public function handle()
{
$this->info('Hello ' .$this->argument('name'));
return 0;
}
}
Which looks a lot more friendly and like how it should in a Laravel setup.
Conclusion
I showed you how you can add command aliases to your Laravel apps in 2 different ways, hoping making the process of running long commands easier. Its definitely saved me time.